Ajv add keyword Nullable (or allowNull)

node v4.9.1
version: master
endpointsharetweet
var Ajv = require('ajv'); var ajv = new Ajv; ajv.addKeyword('allowNull', { type: 'null', metaSchema: { type: 'boolean' }, compile: function(allowNullEnable, parentSchema) { return function(data, dataPath, parentData) { if (allowNullEnable) { return true; } else { if (parentSchema.type == 'null') { return true; } else { return data === null ? false : true; } } } } }); var schema = { type: "object", properties: { file: { type: "string", allowNull: true } } }; var data = { file: null }; var validate = ajv.compile(schema); validate(data) // Expected true
Loading…

no comments

    sign in to comment