addDefinition.schema[requestSchemaKey] = ajv.compile({ type: 'object', properties: addDefinition.schema.properties addDefinition.schema[requestSchemaKey] = ajv.compile(addDefinition.schema.schema.request) addDefinition.schema[isResponseArraySchemaKey] = true addDefinition.schema[responseSchemaKey] = ajv.compile(addDefinition.schema.schema.response)
/** * Validates the top level properties of the config object. * @param {Object} config The config object to validate. * @param {string} source The name of the configuration source to report in any errors. * @returns {void} */ function validateConfigSchema(config, source) { validateSchema = validateSchema || ajv.compile(configSchema); if (!validateSchema(config)) { throw new Error(`${source}:\n\tESLint configuration is invalid:\n${formatErrors(validateSchema.errors)}`); } }
constructor(schema, ajvSettings) { // Used default values if arguments not supplied schema = schema || defaultSchema; ajvSettings = ajvSettings || defaultAJVSettings; this.ajv = new Ajv(ajvSettings); // To use Ajv with draft-06 schemas you need to explicitly add the meta-schema to the validator instance: this.ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json')); this.schema = schema; this.validator = this.ajv.compile(this.schema); }
/** * Validates the non-severity options passed to a rule, based on its schema. * @param {string} id The rule's unique name * @param {array} localOptions The options for the rule, excluding severity * @param {Rules} rulesContext Rule context * @returns {void} */ function validateRuleSchema(id, localOptions, rulesContext) { const schema = getRuleOptionsSchema(id, rulesContext); if (!validators.rules[id] && schema) { validators.rules[id] = ajv.compile(schema); } const validateRule = validators.rules[id]; if (validateRule) { validateRule(localOptions); if (validateRule.errors) { throw new Error(validateRule.errors.map(error => `\tValue "${error.data}" ${error.message}.\n`).join("")); } } }
const createValidator = (schema) => { const validate = ajv.compile(schema); return (name, value) => { if (validate(value)) { return value; } else { const { dataPath, message } = validate.errors[0]; throw new ValidationError({ path: name + dataPath, message, }); } }; }
it("should return a valid JSON schema", function (done) { // Set timeout this.timeout(10000); var schema = JSON.parse(fs.readFileSync(path.join(__dirname, "../", "schema", "events-response.schema.json"), "utf8")); var es = new EventSearch(); es.search({ "lat": 40.710803, "lng": -73.964040, "distance": 100, "accessToken": accessToken }).then( function (events) { console.log("Found " + events.metadata.venues +" venues, thereof " + events.metadata.venuesWithEvents + " with events, and " + events.metadata.events + " events total!"); var validate = ajv.compile(schema); var valid = validate(events); return new Promise(function (resolve, reject) { if (!valid) { console.error(validate.errors); reject(validate.errors); } else { resolve(valid); } }); }).should.be.fulfilled.and.notify(done); });
var validate = ajv.compile(openapiSchema); validate(openapi); var errors = validate.errors;
errors = this.validator.errors; } else { let resourceValidate = this.ajv.compile( this.getSubSchema(resourceType), );
var validate = ajv.compile(jsonSchema); validate(obj); var errors = validate.errors;
return function(data) { for (const schema of schemas) { const validator = ajv.compile(schema);
const validate = ajv.compile(configSchema); const isValidAgainstSchema = validate(config);
addDefinition.schema[requestSchemaKey] = ajv.compile({ type: 'object', properties: addDefinition.schema.properties addDefinition.schema[requestSchemaKey] = ajv.compile(addDefinition.schema.schema.request) addDefinition.schema[isResponseArraySchemaKey] = true addDefinition.schema[responseSchemaKey] = ajv.compile(addDefinition.schema.schema.response)
constructor(schema, ajvSettings) { // Used default values if arguments not supplied schema = schema || defaultSchema; ajvSettings = ajvSettings || defaultAJVSettings; this.ajv = new Ajv(ajvSettings); // To use Ajv with draft-06 schemas you need to explicitly add the meta-schema to the validator instance: this.ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json')); this.schema = schema; this.validator = this.ajv.compile(this.schema); }
errors = this.validator.errors; } else { let resourceValidate = this.ajv.compile( this.getSubSchema(resourceType), );
return function(data) { for (const schema of schemas) { const validator = ajv.compile(schema);