validateData()function

validateData(unsafeData: Data, validators: Validators<T>): T
ParamType
unsafeDataData
The potentially invalid data object. required
validatorsValidators<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.
  • undefined props in the object will be set to the default value of that prop.
  • undefined props after validation will not be set in the output object.
  • Excess keys not present in validators are stripped from the output.
  • Returns the original unsafeData reference unchanged when nothing changed and no excess keys exist.

Examples

validateData({ name: "Alice" }, { name: STRING }) // { name: "Alice" }