|
2 | 2 |
|
3 | 3 | [](https://travis-ci.com/sevenwestmedia-labs/typescript-object-validator)
|
4 | 4 |
|
5 |
| -A simple library for typescript projects for validating object shapes. |
| 5 | +A simple library for typescript projects to validating object shapes. You can use this package to declare the shape you'd like an object to align to. This is handy when you have a complex object (for example, an API response) and want to validate the object, AND have typescript recognize the shape. |
| 6 | + |
| 7 | +## Basic Usage |
| 8 | + |
| 9 | +```typescript |
| 10 | +import { validateObjectShape } from 'typescript-object-validator' |
| 11 | + |
| 12 | +const elephantValidation = validateObjectShape( |
| 13 | + 'Elephant API response' // https://elephant-api.herokuapp.com/elephants/random |
| 14 | + randomElephantResponse, |
| 15 | + { |
| 16 | + _id: 'string', |
| 17 | + index: 'number', |
| 18 | + name: 'string', |
| 19 | + affiliation: 'string', |
| 20 | + species: 'string', |
| 21 | + sex: 'string', |
| 22 | + fictional: 'boolean', |
| 23 | + dob: 'string', |
| 24 | + dod: 'string', |
| 25 | + wikilink: 'string', |
| 26 | + image: 'string', |
| 27 | + note: 'string', |
| 28 | + } |
| 29 | +) |
| 30 | + |
| 31 | +if (elephantValidation.valid === true) { |
| 32 | + |
| 33 | + /* |
| 34 | + At this point, elephantValidation.result will have the type of: |
| 35 | + { |
| 36 | + _id: 'string', |
| 37 | + index: 'number', |
| 38 | + name: 'string', |
| 39 | + affiliation: 'string', |
| 40 | + species: 'string', |
| 41 | + sex: 'string', |
| 42 | + fictional: 'boolean', |
| 43 | + dob: 'string', |
| 44 | + dod: 'string', |
| 45 | + wikilink: 'string', |
| 46 | + image: 'string', |
| 47 | + note: 'string', |
| 48 | + } |
| 49 | + */ |
| 50 | + |
| 51 | + console.log(elephantValidation.result.name) |
| 52 | + // -> "Packy" |
| 53 | +} |
| 54 | + |
| 55 | +``` |
| 56 | + |
| 57 | +## API |
| 58 | + |
| 59 | +`validateObjectShape` Can be used to validate an object: |
| 60 | + |
| 61 | +```typescript |
| 62 | +import { validateObjectShape } from 'typescript-object-validator' |
| 63 | + |
| 64 | +validateObjectShape( |
| 65 | + objectDescription, |
| 66 | + validationItem, |
| 67 | + expectedObjectShape, |
| 68 | + validationOptions |
| 69 | +) |
| 70 | +``` |
| 71 | + |
| 72 | +- **objectDescription** (required - string): A description which is used in error messages when the object does not validate |
| 73 | +- **validationItem** (required - object): An object you want to validate |
| 74 | +- **expectedObjectShape** (required - object): A definition of what shape the object should match |
| 75 | +- **validationOptions** (required - object): A set of options to change the validation behaviour, [see more](#Options) |
| 76 | + |
| 77 | +The result of the validation is an object with a `valid` property which flags if the validation succeeded or failed, a `result` object which is the validated and typed object, or an `errors` array with error results. |
| 78 | + |
| 79 | +```typescript |
| 80 | +const validationResult = validateObjectShape( |
| 81 | + objectDescription, |
| 82 | + validationItem, |
| 83 | + expectedObjectShape, |
| 84 | + validationOptions |
| 85 | +) |
| 86 | + |
| 87 | +// If the validation is successful |
| 88 | +{ |
| 89 | + valid: true, |
| 90 | + result: { ...validatedItem } // (the object you passed in, but typed!) |
| 91 | +} |
| 92 | + |
| 93 | +// If the validation fails |
| 94 | +{ |
| 95 | + valid: false, |
| 96 | + errors: [ |
| 97 | + 'Expected Test obj.two to be type: number, was string', |
| 98 | + ] |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +`validateObjectShape` will coerce values for you if it's able to. For example if you say the property `age` is a number, but it's passed in as a string, `validateObjectShape` will try convert it to a number for you in the `result` object: |
| 103 | + |
| 104 | +```typescript |
| 105 | +const validationResult = validateObjectShape( |
| 106 | + 'Coercion Example', |
| 107 | + { age: '30' }, |
| 108 | + { age: number } |
| 109 | +) |
| 110 | + |
| 111 | +if (validationResult.valid === true) { |
| 112 | + // validationResult.result.age -> 30 |
| 113 | + typeof validationResult.result.age === 'number' |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +The library also alows you to test nested objects and arrays, for example: |
| 118 | + |
| 119 | +```typescript |
| 120 | +const validationResult = validateObjectShape( |
| 121 | + 'Nested Example', |
| 122 | + { |
| 123 | + age: '30', |
| 124 | + meta: { group: 'Staff' } |
| 125 | + names: [ |
| 126 | + { fname: 'Jeff', lname: 'Thompson' }, |
| 127 | + { fname: 'Jeff', lname: 'Thompson' } |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + age: number, |
| 132 | + meta: { group: 'string' } |
| 133 | + names: arrayOf({ fname: 'string', lname: 'string' }) |
| 134 | + } |
| 135 | +) |
| 136 | +``` |
| 137 | + |
| 138 | +### Validation Types |
| 139 | + |
| 140 | +There are a number of basic validation types available to use, and some helpers which allow you build more complex types: |
| 141 | + |
| 142 | +Basic types |
| 143 | + |
| 144 | +- **string**: Matches a string |
| 145 | +- **number**: Matches a number |
| 146 | +- **boolean**: Matches a boolean |
| 147 | +- **unknown**: Matches an unknown type (will skip validation on that property) |
| 148 | + |
| 149 | +This package also understands more complex types such as arrays and optional types. You can import helper functions to assist you in building these. |
| 150 | + |
| 151 | +Complex Types: |
| 152 | + |
| 153 | +- **arrayOf** ('string'): Array of some basic type |
| 154 | +- **optional** ('string'): Makes the property your testing optional. The test will only run if the property exists |
| 155 | + |
| 156 | +```typescript |
| 157 | +const result = validateObjectShape( |
| 158 | + 'Test obj', |
| 159 | + { |
| 160 | + one: 'string value', |
| 161 | + two: 2, |
| 162 | + three: true, |
| 163 | + four: ['1', '2'], |
| 164 | + five: [true, false], |
| 165 | + six: [1, 2], |
| 166 | + seven: 'whatever' |
| 167 | + }, |
| 168 | + { |
| 169 | + one: 'string', |
| 170 | + two: 'number', |
| 171 | + three: 'boolean', |
| 172 | + four: arrayOf('string'), |
| 173 | + five: arrayOf('boolean'), |
| 174 | + six: arrayOf('number'), |
| 175 | + seven: 'unknown', |
| 176 | + eight: optional('boolean') |
| 177 | + } |
| 178 | +) |
| 179 | +``` |
| 180 | + |
| 181 | +### Options |
| 182 | + |
| 183 | +- **coerceValidObjectIntoArray** (boolean): If you are validating an array with `arrayOf`, setting this to `true` will convert those properties to arrays for you, rather than returning an error. This is useful for example when converting from xml to json. |
| 184 | + |
| 185 | +```typescript |
| 186 | +const validationResult = validateObjectShape( |
| 187 | + 'Coercion Example', |
| 188 | + { names: { fname: 'Jeff', lname: 'Thompson' } }, |
| 189 | + { names: arrayOf({ fname: 'string', lname: 'string' }) }, |
| 190 | + { coerceValidObjectIntoArray: true } |
| 191 | +) |
| 192 | + |
| 193 | +/* |
| 194 | + Converts names into an array. validationResult.result: |
| 195 | + { names: [{ fname: 'Jeff', lname: 'Thompson' }] } |
| 196 | +*/ |
| 197 | +``` |
0 commit comments