README
API validator
A module for validating DADI API documents
Overview
DADI API is a high-performance RESTful layer designed in support of API-first development and COPE. With this module, you can perform validation of API documents within your project using the same logic as used by the DADI API core application.
Installation
Install the module via npm:
npm install @dadi/api-validator
Usage
The module exports a constructor method that expects the following named parameters relating to the instance of DADI API you're working with:
i18nFieldCharacter: the character used to represent the language variation of a field, as defined by thei18n.fieldCharacterconfiguration property (default::)internalFieldPrefix: the character used to prefix internal API fields, as defined by theinternalFieldPrefixconfiguration property (default:_)
const Validator = require('@dadi/api-validator')
const myValidator = new Validator({
i18nFieldCharacter: ':',
internalFieldPrefix: '_'
})
The Validator class contains the following methods.
validateAccessMatrix(matrix, fieldName)
Validates an access matrix. An optional fieldName can be supplied, which will be used in the error objects as the path of the field being validated.
// Throws error:
// > [
// > {"field": "foo.invalidType", "code": "ERROR_INVALID_ACCESS_TYPE"},
// > {"field": "foo.update.invalidKey", "code": "ERROR_INVALID_ACCESS_VALUE"}
// > ]
myValidator.validateAccessMatrix(
{
create: true,
invalidType: true,
update: {
invalidKey: true
}
},
'foo'
)
// > undefined
myValidator.validateAccessMatrix({
create: true,
delete: {
filter: {
someField: 'someValue'
}
}
})
validateDocument({document, isUpdate, schema})
Validates a document against a collection schema. It returns a Promise that is resolved with undefined if no validation errors occur, or rejected with an array of errors if validation fails.
If isUpdate is set to true, the method does not throw a validation error if a required field is missing from the candidate document, because it is inferred that a partial update, as opposed to a full document, is being evaluated.
const mySchema = {
title: {
type: 'string',
validation: {
minLength: 10
}
}
}
// Rejected Promise:
// > [{"field": "title", "code": "ERROR_MIN_LENGTH"}]
myValidator
.validateDocument({
document: {
title: 'great'
},
schema: mySchema
})
.catch(console.log)
// Resolved Promise:
// > undefined
myValidator.validateDocument({
document: {
title: 'super amazing!'
},
schema: mySchema
})
validateDocuments({documents, schema})
Same as validateDocument but expects an array of documents (as the documents property) and performs validation on each one of them, aborting the process once one of the documents fails validation.
validateSchemaField(name, schema)
Validates a field schema, evaluating whether name is a valid field name and whether schema is a valid schema object.
// Rejected Promise:
// > [{"field": "fields.title", "code": "ERROR_MISSING_FIELD_TYPE"}]
myValidator
.validateSchemaField('title', {
validation: {
minLength: 10
}
})
.catch(console.log)
// Resolved Promise:
// > undefined
myValidator.validateDocument({
type: 'string',
validation: {
minLength: 10
}
})
validateSchemaFieldName(name)
Validates a field name.
// Rejected Promise:
// > [{"field": "fields.$no-good