Validate and translate JSON objects for Document Databases like MongoDB or Amazon's DynamoDB.
npm install --save @fyresite/document_validator
Validations are handled using @fyresite/object-validator and utilizes the validator node module.
module.exports = function (Types, Op) {
return {
"name": {
"first": {
"type": Types.STRING,
"required": true
},
"middle": Types.STRING,
"last": {
"type": Types.STRING,
"required": true
}
},
validateField: {
type: Types.STRING,
validate: {
"method": "matches",
"pattern": "^([a-z ])+$"
}
},
"myInt":{
type: Types.INTEGER,
"required": {
myFloat:Op.eq(4.5)
}
},
"myFloat": Types.FLOAT
"email": {
"type": Types.STRING,
"required": true
},
"phone": {
"type": Types.STRING,
"required": true
},
"dob": {
"type": Types.DATE,
"required": true
}
}
}
var { Validator } = require('@fyresite/document_validator');
var userValidator = new Validator('user');
var userValid = {
"name": {
"first": "Mike",
"middle": "T",
"last": "Jones"
},
"email": "email@example.com",
"phone": "2813308004",
"dob": new Date("1996-11-09")
};
var userInvalid = {
"name": {
"first": "Mike",
"middle": "T",
"last": "Jones"
},
"email": "email@example.com",
"phone": 344,
"dob": new Date("1996-11-09"),
"address": {
"street": "2212 S Banner St",
"street2":"Strong",
"city": "Gilbert",
"state": "AZ",
"zip": 85296
}
};
// Returns emptyObject {} signifying the document was valid
var invalidFields = userValidator.validate(userValid, 'v1');
// Returns object containing all of the validation errors that occurred
var invalidFields = userValidator.validate(userInvalid, 'v1');
var userValidator = new Validator('user', { schemaPath: 'schemas', isS3: true, bucket: 'model-schemas', profile: 'test-profile' });
var userValid = {
"name": {
"first": "Mike",
"middle": "T",
"last": "Jones"
},
"email": "email@example.com",
"phone": "2813308004",
"dob": new Date("1996-11-09"),
"_v" : 1
};
var userInvalid = {
"name": {
"first": "Mike",
"middle": "T",
"last": "Jones"
},
"email": "email@example.com",
"phone": 344,
"dob": new Date("1996-11-09"),
"address": {
"street": "2212 S Banner St",
"street2":"Strong",
"city": "Gilbert",
"state": "AZ",
"zip": 85296
},
"_v" : 1
};
userValidator.init()
.then(() => {
var invalidFields;
// Returns emptyObject {} signifying the document was valid
invalidFields = userValidator.validate(userValid, 'v1');
// Returns object containing all of the validation errors that occurred
invalidFields = userValidator.validate(userInvalid, 'v1');
})
.catch(err => {
console.error(err);
});
Class used validate and translate documents
Kind: global class
- Validator
- new Validator(schemaName, [config])
- .init() ⇒
Promise
- .validate(document, version) ⇒
Object
- .validateKeys(fields, version) ⇒
Object
- .translate(document, version) ⇒
Object
Create a validator.
Param | Type | Default | Description |
---|---|---|---|
schemaName | string |
Name of the schema to validate. | |
[config] | Object |
Custom configuration options. | |
config.schemaPath | string |
"APP_ROOT/schemas" |
the path to the schemas folder |
config.isS3 | boolean |
true |
Define if schema location is a s3 bucket |
config.s3Bucket | string |
Name of the s3 bucket to connect the schemas are located | |
config.awsProfile | string |
The AWS Profile you want to use to connect to the bucket |
Used to fetch schema from s3, the instance validate function must be used inside .then() to guarantee that schemas are loaded before validating
Kind: instance method of Validator
Validates document against specific version of the schema
Kind: instance method of Validator
Param | Type | Description |
---|---|---|
document | Object |
Document to be validated |
version | string |
version to be validated. |
Validates keys for document against specific version of the schema
Kind: instance method of Validator
Param | Type | Description |
---|---|---|
fields | Object |
Document to be validated |
version | string |
version to be validated. |
Translates document to version specified the documents version is automatically read through the _v property on the document
Kind: instance method of Validator
Param | Type | Description |
---|---|---|
document | Object |
Document to be validated |
version | string |
version to be translated to. |
Defines the validation rules that will be applied to the document using the Validator
// Export a function (Types, Op) that returns a json
module.exports = function(Types, Op) {
return {
/*
Fields can be defined by setting the ket to the field
name and the type as the value
*/
fieldName: Types.STRING
// Or pass an object for more advanced options
advancedField: {
type: Types.INTEGER, //Type of field *required*
/*
flag that tells validator to return error if field is not provided.
The value can be
*/
required: true,
//Default value to use if field is not provided
default: 0,
/*
Validation object that is used for advanced validation. Methods are based on the validator npm package by cohara87.
For further documentation refer to @fyresite/object_validator
*/
validate: {
"method": "validator method", /** String **/
"param1": value,
"param2": value
}
}
}
}
An function can be passed
module.exports = function(Types, Op) {
return {
field1 : Types.NUMBER
}
}
Class used to provide conditions to require flags
Kind: global class
- Op
- .any(conditions) ⇒
function
- .not(opFunction) ⇒
function
- .set() ⇒
function
- .eq(value) ⇒
function
- .gt(value) ⇒
function
- .gte(value) ⇒
function
- .lt(value) ⇒
function
- .lte(value) ⇒
function
- .any(conditions) ⇒
Checks if the documents value matches any of the conditions
Kind: static method of Op
Param | Type | Description |
---|---|---|
conditions | array |
Array of conditions to match on the Op functions are valid conditions |
Accepts Op Function and returns opposite value
Kind: static method of Op
Param | Type | Description |
---|---|---|
opFunction | function |
valid Op function |
Checks if field is set
Kind: static method of Op
Checks if field equals value
Kind: static method of Op
Param | Type |
---|---|
value | number | string | boolean |
Checks if fields value is greater than the provided value
Kind: static method of Op
Param | Type |
---|---|
value | number |
Checks if fields value is greater than or equal to provided value
Kind: static method of Op
Param | Type |
---|---|
value | number |
Checks if fields value is less than the provided value
Kind: static method of Op
Param | Type |
---|---|
value | number |
Checks if fields value is less than or equal to provided value
Kind: static method of Op
Param | Type |
---|---|
value | number |