@tailored-apps/dedition
v1.0.0
Published
Wrapper for AJV which can read all json files and adds them as schemas
Downloads
67
Readme
DEDITION
- This package combines
ajv
(https://www.npmjs.com/package/ajv) andajv-error
(https://www.npmjs.com/package/ajv-errors) - It also offers functionality to add validation schemas from
.json
files
Usage
import validator from 'dedition'
Initialize
const valid = validator({ ajvOptions = {}, logger = createDefaultLogger()} = {})
ajvOptions
are all the options which are normally passed toajv
(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
schemaName
Identifier for the schemaschemaObj
is aajv
validation schema (https://www.npmjs.com/package/ajv#validation-keywords)
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 schemaobj
: 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
)