validateData()function
validateData(unsafeData: Data, validators: Validators<T>): T
| Param | Type | |
|---|---|---|
unsafeData | Data | The potentially invalid data object. required |
validators | Validators<T> | The set of named validators to validate each prop with. required |
| Return | |
|---|---|
T | Valid object. |
| Throws | |
|---|---|
unknown | string if one or more props did not validate (one "key: message" line per failure, joined by newlines). |
Validate a data object with a set of validators.
- Defined props in the object will be validated against the corresponding validator.
undefinedprops in the object will be set to the default value of that prop.undefinedprops after validation will not be set in the output object.- Excess keys not present in
validatorsare stripped from the output. - Returns the original
unsafeDatareference unchanged when nothing changed and no excess keys exist.
Examples
validateData({ name: "Alice" }, { name: STRING }) // { name: "Alice" }