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) const error = new Error(ajv.errorsText(schema[requestSchemaKey].errors, { dataVar: 'pattern' })) error.validation = schema[requestSchemaKey].errors return { ajv.errorsText(validate.errors, { dataVar: '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)}`); } }
validate.errors.forEach(function (error) { if (error.keyword === 'additionalProperties') { console.warn('MercadoPago SDK: "' + error.params.additionalProperty + '": is not a valid property.'); } else { validationErrors.push(error); } });
/** * validate json schema * @param {obj} paramObj * @param {obj} schema * @returns {{valid: boolean, err_msg?: string}} */ function validateSchema(paramObj, schema){ const ajv = new Ajv(); const valid = ajv.validate(schema, paramObj); if (!valid) return { valid, err_msg: `Input parsing error: ${ajv.errors[0].dataPath} ${ajv.errors[0].message}.` }; return { valid }; }
ajv.addFormat('city', function (city) { console.log('XXX', city) return true })
static createValidator() { return new AjvValidator({ onCreateAjv: (ajv) => { // Here you can modify the `Ajv` instance. ajv.addFormat('url', httpUrlPattern); }, options: { allErrors: true, validateSchema: false, ownProperties: true, v5: true, }, }); }
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); }
/** * override allOf keywords support removeAdditional option * @param {import('ajv').Ajv} ajv */ function allOfRA(ajv) { ajv.removeKeyword("allOf"); ajv.addKeyword("allOf", { macro: function(schema) { return merge({}, ...schema); }, metaSchema: { type: "array", items: [{ type: "object" }], }, errors: true, }); }
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) const error = new Error(ajv.errorsText(schema[requestSchemaKey].errors, { dataVar: 'pattern' })) error.validation = schema[requestSchemaKey].errors return { ajv.errorsText(validate.errors, { dataVar: 'response' })
ajv.addFormat('city', function (city) { console.log('XXX', city) return true })
ajv.addFormat('city', function (city) { console.log('XXX', city) return true })
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); }
ajv.addFormat('city', function (city) { console.log('XXX', city) return true })