Validator
Class Name |
Validator |
|---|---|
Extends |
|
Source |
|
Examples |
The Validator module can be used to verfiy given JSON schemas.
constructor
new Validator(options);
Creates a new Validator instance.
Parameters
options-ValidatorOptions: options for Validator constructor.schema-any: the validation schema definitionlog-Function(optional): function to use for logging:(message, level) => {...}logLevel-LogLevel(optional): messages with this level will be logged withloglogLog-LogLogInterface(optional): container for collecting log messageslogLogLevel-LogLevel(optional): messages with this level will be pushed tologLog
Returns
Validator instance
Example
const nameResolver = new Validator({
schema
});
isSchemaCorrect
Validator.isSchemaCorrect(schema);
Checks if the given ajv schema is correct and returns an array of ajv errors, when the schema is invalid.
Parameters
schema-any: schema to be validated
Returns
bool | AjvError: true if data is valid, array of object if validation is failed
validate
validator.validate(data);
validate a given data object with the instantiated schema
Parameters
data-any: to be validated data
Returns
bool | AjvError: true if data is valid, array of object if validation is failed
getErrorsAsText
validator.getErrorsAsText();
returns errors as text if previous validation was failed
Returns
string: all previous validation errors concatenated as readable string