change ajv draft 4 to ajv 6 and validate using schema for 04

change ajv draft 4 to ajv 6 and validate using schema for 04
This commit is contained in:
Luis Tejeda
2022-02-04 12:45:52 -06:00
parent e2d324aa4a
commit 760bcfdebe
7 changed files with 60 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
const Ajv = require('ajv-draft-04'),
addFormats = require('ajv-formats');
const Ajv = require('ajv-06'),
draft4MetaSchema = require('ajv-06/lib/refs/json-schema-draft-04.json');
/**
* Used to validate schema against a value.
@@ -20,9 +20,10 @@ function validateSchemaAJVDraft04 (schema, valueToUse) {
ajv = new Ajv({
// check all rules collecting all errors. instead returning after the first error.
allErrors: true,
strict: false
strict: false,
schemaId: 'id'
});
addFormats(ajv);
ajv.addMetaSchema(draft4MetaSchema);
validate = ajv.compile(schema);
validate(valueToUse);
}
@@ -31,6 +32,19 @@ function validateSchemaAJVDraft04 (schema, valueToUse) {
// input was invalid. Don't throw mismatch
return { filteredValidationError };
}
if (validate.errors && validate.errors.length > 0) {
let mapped = validate.errors.map(((error) => {
return {
instancePath: error.dataPath,
keyword: error.keyword,
message: error.message,
params: error.params,
schemaPath: error.schemaPath
};
}));
validate.errors = mapped;
}
return { filteredValidationError, validate };
}

View File

@@ -3190,7 +3190,7 @@ module.exports = {
mismatchObj = _.assign({
property: property,
transactionJsonPath: jsonPathPrefix + ajvError.instancePath,
transactionJsonPath: jsonPathPrefix + formatDataPath(ajvError.instancePath),
schemaJsonPath: schemaPathPrefix + '.' + localSchemaPath
}, ajvValidationError(ajvError, { property, humanPropName }));

24
package-lock.json generated
View File

@@ -389,10 +389,23 @@
"uri-js": "^4.2.2"
}
},
"ajv-draft-04": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/ajv-draft-04/-/ajv-draft-04-1.0.0.tgz",
"integrity": "sha512-mv00Te6nmYbRp5DCwclxtt7yV/joXJPGS7nM+97GdxvuttCOfgI3K4U25zboyeX0O+myI8ERluxQe5wljMmVIw=="
"ajv-06": {
"version": "npm:ajv@6.12.5",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-6.12.5.tgz",
"integrity": "sha512-lRF8RORchjpKG50/WFf8xmg7sgCLFiYNNnqdKflk63whMQcWR5ngGjiSXkL9bjxy6B2npOK2HSMN49jEBMSkag==",
"requires": {
"fast-deep-equal": "^3.1.1",
"fast-json-stable-stringify": "^2.0.0",
"json-schema-traverse": "^0.4.1",
"uri-js": "^4.2.2"
},
"dependencies": {
"json-schema-traverse": {
"version": "0.4.1",
"resolved": "https://registry.npmjs.org/json-schema-traverse/-/json-schema-traverse-0.4.1.tgz",
"integrity": "sha512-xbbCH5dCYU5T8LcEhhuh7HJ88HXuW3qsI3Y0zOZFKfZEHcpWiHU/Jxzk629Brsab/mMiHQti9wMP+845RPe3Vg=="
}
}
},
"ajv-formats": {
"version": "2.1.1",
@@ -1197,8 +1210,7 @@
"fast-json-stable-stringify": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz",
"integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==",
"dev": true
"integrity": "sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw=="
},
"fast-levenshtein": {
"version": "2.0.6",

View File

@@ -117,7 +117,7 @@
},
"dependencies": {
"ajv": "8.1.0",
"ajv-draft-04": "1.0.0",
"ajv-06": "npm:ajv@6.12.5",
"ajv-formats": "2.1.1",
"async": "3.2.1",
"commander": "2.20.3",

View File

@@ -80,8 +80,9 @@ describe('project repository', function () {
// Unskip before merging
it('must point to a valid and precise (no * or ^) semver', function () {
json.dependencies && Object.keys(json.dependencies).forEach(function (item) {
expect(json.dependencies[item]).to.match(new RegExp('^((\\d+)\\.(\\d+)\\.(\\d+))(?:-' +
'([\\dA-Za-z\\-]+(?:\\.[\\dA-Za-z\\-]+)*))?(?:\\+([\\dA-Za-z\\-]+(?:\\.[\\dA-Za-z\\-]+)*))?$'));
expect(json.dependencies[item]).to.match(new RegExp('(^((\\d+)\\.(\\d+)\\.(\\d+)|' +
'(^npm:[\\dA-Za-z\\-]+@(\\d+)\\.(\\d+)\\.(\\d+)))(?:-([\\dA-Za-z\\-]+(?:\\.[\\dA-Za-z\\-]+)*))?' +
'(?:\\+([\\dA-Za-z\\-]+(?:\\.[\\dA-Za-z\\-]+)*))?)$'));
});
});
});
@@ -93,8 +94,9 @@ describe('project repository', function () {
it('must point to a valid and precise (no * or ^) semver', function () {
json.devDependencies && Object.keys(json.devDependencies).forEach(function (item) {
expect(json.devDependencies[item]).to.match(new RegExp('^((\\d+)\\.(\\d+)\\.(\\d+))(?:-' +
'([\\dA-Za-z\\-]+(?:\\.[\\dA-Za-z\\-]+)*))?(?:\\+([\\dA-Za-z\\-]+(?:\\.[\\dA-Za-z\\-]+)*))?$'));
expect(json.devDependencies[item]).to.match(new RegExp('(^((\\d+)\\.(\\d+)\\.(\\d+)|' +
'(^npm:[\\dA-Za-z\\-]+@(\\d+)\\.(\\d+)\\.(\\d+)))(?:-([\\dA-Za-z\\-]+(?:\\.[\\dA-Za-z\\-]+)*))?' +
'(?:\\+([\\dA-Za-z\\-]+(?:\\.[\\dA-Za-z\\-]+)*))?)$'));
});
});

View File

@@ -16,6 +16,21 @@ describe('formatData method', function() {
formattedDataPath = formatDataPath(instancePath);
expect(formattedDataPath).to.be.equal(expectedDataPath);
});
it('Should return the same string when input is a data path ".id" ', function() {
const inputAlreadyDataPath = '.id',
expectedDataPath = '.id',
formattedDataPath = formatDataPath(inputAlreadyDataPath);
expect(formattedDataPath).to.be.equal(expectedDataPath);
});
it('Should return the same string when input is a data path ".test[3]" ', function() {
const inputAlreadyDataPath = '.test[3]',
expectedDataPath = '.test[3]',
formattedDataPath = formatDataPath(inputAlreadyDataPath);
expect(formattedDataPath).to.be.equal(expectedDataPath);
});
});
describe('handleExclusiveMaximum method', function() {

View File

@@ -227,7 +227,7 @@ describe('validateSchema', function () {
name: 'dolor consectetur Excepteur'
},
result = validateSchema(schema, valueToUse);
expect(result[0].instancePath).equal('/id');
expect(result[0].instancePath).equal('.id');
});
it('should return no errors correct schema value $schema pointing to draft 06', function () {