Ajv SO question 39167985

node v4.9.1
version: master
endpointsharetweet
var Ajv = require('ajv'); var ajv = new Ajv; ajv.addKeyword('validateLocales', { type: 'object', compile: function(schema) { return function(data, dataPath, parentData) { for (var prop in data) { if (parentData[schema.localesProperty].indexOf(prop) == -1) { return false; } } return true; } }, metaSchema: { type: 'object', properties: { localesProperty: { type: 'string' } }, additionalProperties: false } }); var schema = { "$schema": "http://json-schema.org/draft-04/schema#", "description": "", "type": "object", "properties": { "languages": { "type": "array", "items": { "type": "string" } }, "file": { "type": "object", "validateLocales": { "localesProperty": "languages" }, "additionalProperties": { "type": "string" } } } }; var data = { "languages": ['en', 'ch'], "file": { "en": "file1", "ch": "file2" } }; var validate = ajv.compile(schema); console.log(validate(data));
Loading…

no comments

    sign in to comment