DEDITION
Usage
import validator from 'dedition'
Initialize
const valid = validator({ ajvOptions = {}, logger = createDefaultLogger()} = {})
ajvOptions are all the options which are normally passed to ajv (https://www.npmjs.com/package/ajv#options)
logger can be passed optionally (i.e.: a winston instance), if not passed a default logger is created
There are two ajv-options set at any time:
allErrors = true
jsonPointers = true
These are mandatory and CANNOT be overwritten, since ajv-errors needs these two options to function
After initializing validator you have access to several functions
Functions
addSchema(schemaName: string, schemaObj: object)
Adds a given schema to the ajv instance using the passed schemaName as identifier
Returns: void
async addSchemaFromFile(filePath: string)
Adds the schema from the given filePath (must be a valid .json file) to the ajv instance.
The identifier is the fileName without the .json-extension
i.e.: Person.json -> Identifier: Person
filePath is the path to the .json file
Returns: void
async addSchemaFromFolder(folderPath: string)
Adds all the schemas from the given folderPath (only files inside this folder with extension .json are recognized) to the ajv instance.
The identifier is the file name without the .json-extenision
i.e.: Person.json -> Identifier: Person
folderPath is the path to the folder where the schema files are located
Returns: void
validate(schemaName: string, obj: object)
Validates an Object with the given schema which is identified by the schema identifier string
schemaName: Name of the schema
obj: Object to validate
Returns: true if valid, otherwise 400: BadRequestError (from package http-errors)
removeAllSchemas()
Removes all the schemas from the ajv instance
Returns: void
schemaExists(schemaName: string)
Checks wheter an schema exists or not
schemaName: Name of the schema
Returns: True or False wheter the schema exists or not
getSchema(schemaName: string)
Returns the schema for a given schemaName
schemaName: Name of the schema
Returns: Validation Schema object or 500: InternalServerError (from package http-errors)