Skip to content

Commit

Permalink
Merge pull request #369 from apiaryio/remove-amanda
Browse files Browse the repository at this point in the history
Removes JSON Schema Draft 3 support
  • Loading branch information
artem-zakharchenko authored Jan 30, 2020
2 parents 3e9658b + 6712471 commit e63e141
Show file tree
Hide file tree
Showing 12 changed files with 21 additions and 515 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ The validation `result` against the given JSON Schema will look as follows:
}
```

### Supported JSON Schema versions

- [JSON Schema Draft 7](https://json-schema.org/specification-links.html#draft-7)
- [JSON Schema Draft 6](https://json-schema.org/specification-links.html#draft-6)
- [JSON Schema Draft 4](https://json-schema.org/specification-links.html#draft-4)

## Examples

Take a look at the [Gherkin](https://cucumber.io/docs/gherkin/) specification, which describes on examples how validation of each field behaves:
Expand Down
174 changes: 0 additions & 174 deletions lib/meta-schema-v3.js

This file was deleted.

97 changes: 3 additions & 94 deletions lib/validators/json-schema-legacy.js
Original file line number Diff line number Diff line change
@@ -1,68 +1,17 @@
const amanda = require('amanda');
const tv4 = require('tv4');
const jsonPointer = require('json-pointer');

const { JsonSchemaValidator, META_SCHEMA } = require('./json-schema-next');
const { ValidationErrors } = require('./validation-errors');
const toGavelResult = require('../utils/to-gavel-result');
const warnOnDeprecation = require('../utils/warn-on-deprecation');

/**
* Returns a proper article for a given string.
* @param {string} str
* @returns {string}
*/
function getArticle(str) {
return ['a', 'e', 'i', 'o', 'u'].includes(str.toLowerCase()) ? 'an' : 'a';
}

const jsonSchemaOptions = {
singleError: false,
messages: {
minLength: (prop, val, validator) =>
`The ${prop} property must be at least ${validator} characters long (currently ${val.length} characters long).`,
maxLength: (prop, val, validator) =>
`The ${prop} property must not exceed ${validator} characters (currently${val.length} characters long).`,
length: (prop, val, validator) =>
`The ${prop} property must be exactly ${validator} characters long (currently ${val.length} characters long).`,
format: (prop, val, validator) =>
`The ${prop} property must be ${getArticle(
validator[0]
)} ${validator} (current value is ${JSON.stringify(val)}).`,
type: (prop, val, validator) =>
`The ${prop} property must be ${getArticle(
validator[0]
)} ${validator} (current value is ${JSON.stringify(val)})."`,
except: (prop, val) => `The ${prop} property must not be ${val}.`,
minimum: (prop, val, validator) =>
`The minimum value of the ${prop} must be ${validator} (current value is ${JSON.stringify(
val
)}).`,
maximum: (prop, val, validator) =>
`The maximum value of the ${prop} must be ${validator} (current value is ${JSON.stringify(
val
)}).`,
pattern: (prop, val, validator) =>
`The ${prop} value (${val}) does not match the ${validator} pattern.`,
maxItems: (prop, val, validator) =>
`The ${prop} property must not contain more than ${validator} items (currently contains ${val.length} items).`,
minItems: (prop, val, validator) =>
`The ${prop} property must contain at least ${validator} items (currently contains ${val.length} items).`,
divisibleBy: (prop, val, validator) =>
`The ${prop} property is not divisible by ${validator} (current value is ${JSON.stringify(
val
)}).`,
uniqueItems: (prop) => `All items in the ${prop} property must be unique.`
}
};

class JsonSchemaLegacy extends JsonSchemaValidator {
validateSchema() {
const { jsonSchema, jsonMetaSchema } = this;

// In case schema version is unidentified,
// assume JSON Schema Draft V3.
const metaSchema = jsonMetaSchema || META_SCHEMA.draftV3;
// Set the default JSON Schema version if no explicit
// version is provided.
const metaSchema = jsonMetaSchema || META_SCHEMA.draftV4;

tv4.reset();
tv4.addSchema('', metaSchema);
Expand All @@ -76,12 +25,6 @@ class JsonSchemaLegacy extends JsonSchemaValidator {
const parsedData = this.parseData(data);

switch (this.jsonSchemaVersion) {
case 'draftV3':
warnOnDeprecation(
'JSON Schema Draft V3 is deprecated. Please use a newer version of JSON Schema (Draft V4-7).'
);

return this.validateUsingAmanda(parsedData);
case 'draftV4':
return this.validateUsingTV4(parsedData);
default:
Expand All @@ -91,40 +34,6 @@ class JsonSchemaLegacy extends JsonSchemaValidator {
}
}

validateUsingAmanda(data) {
let errors = {
length: 0,
errorMessages: {}
};

try {
amanda.validate(data, this.jsonSchema, jsonSchemaOptions, (error) => {
if (error && error.length > 0) {
for (let i = 0; i < error.length; i++) {
if (error[i].property === '') {
error[i].property = [];
}
}

errors = new ValidationErrors(error);
}
});
} catch (internalError) {
errors = new ValidationErrors({
'0': {
property: [],
attributeValue: true,
message: `Validator internal error: ${internalError.message}`,
validatorName: 'error'
},
length: 1,
errorMessages: {}
});
}

return toGavelResult(errors);
}

validateUsingTV4(data) {
const result = tv4.validateMultiple(data, this.jsonSchema);
const validationErrors = result.errors.concat(result.missing);
Expand Down
22 changes: 9 additions & 13 deletions lib/validators/json-schema-next.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,16 @@ const metaSchemaV6 = require('ajv/lib/refs/json-schema-draft-06.json');
const metaSchemaV7 = require('ajv/lib/refs/json-schema-draft-07.json');

const metaSchemaV4 = require('../meta-schema-v4');
const metaSchemaV3 = require('../meta-schema-v3');
const errors = require('../errors');
const parseJson = require('../utils/parseJson');

const SCHEMA_VERSIONS = {
draftV3: 'http://json-schema.org/draft-03/schema',
draftV4: 'http://json-schema.org/draft-04/schema',
draftV6: 'http://json-schema.org/draft-06/schema',
draftV7: 'http://json-schema.org/draft-07/schema'
};

const META_SCHEMA = {
draftV3: metaSchemaV3,
draftV4: metaSchemaV4,
draftV6: metaSchemaV6,
draftV7: metaSchemaV7
Expand Down Expand Up @@ -46,16 +43,15 @@ const getImplicitSchemaVersion = (jsonSchema) => {
* without the explicit version.
*/
const getImplicitLegacySchemaVersion = (jsonSchema) => {
const [schemaVersion] = [
['draftV3', metaSchemaV3],
['draftV4', metaSchemaV4]
].find(([_, metaSchema]) => {
tv4.reset();
tv4.addSchema('', metaSchema);
tv4.addSchema(metaSchema.$schema, metaSchema);
const validationResult = tv4.validateResult(jsonSchema, metaSchema);
return validationResult.valid;
}) || [null, null];
const [schemaVersion] = [['draftV4', metaSchemaV4]].find(
([_, metaSchema]) => {
tv4.reset();
tv4.addSchema('', metaSchema);
tv4.addSchema(metaSchema.$schema, metaSchema);
const validationResult = tv4.validateResult(jsonSchema, metaSchema);
return validationResult.valid;
}
) || [null, null];

return schemaVersion;
};
Expand Down
2 changes: 1 addition & 1 deletion lib/validators/json-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class JsonSchema {
constructor(jsonSchema) {
const resolvedJsonSchema = resolveJsonSchema(jsonSchema);
const jsonSchemaVersion = getSchemaVersion(resolvedJsonSchema);
const isLegacySchema = ['draftV3', 'draftV4'].includes(jsonSchemaVersion);
const isLegacySchema = ['draftV4'].includes(jsonSchemaVersion);

// Instantiate different JSON Schema validators
// based on the JSON Schema Draft version.
Expand Down
5 changes: 0 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
},
"dependencies": {
"ajv": "6.10.2",
"amanda": "1.0.1",
"caseless": "0.12.0",
"clone": "2.1.2",
"commander": "3.0.2",
Expand Down
Loading

0 comments on commit e63e141

Please sign in to comment.