Revert "Bump jsonschema from 1.2.6 to 1.4.0"

This commit is contained in:
Chuan-kai Lin 2021-08-09 12:03:58 -07:00 committed by GitHub
parent 8ddd620de3
commit 8068352efa
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 168 additions and 533 deletions

View file

@ -16,13 +16,13 @@ attribute.ignoreProperties = {
'description': true,
'title': true,
// arguments to other properties
'exclusiveMinimum': true,
'exclusiveMaximum': true,
'additionalItems': true,
'then': true,
'else': true,
// special-handled properties
'$schema': true,
'$ref': true,
'extends': true,
'extends': true
};
/**
@ -47,9 +47,7 @@ validators.type = function validateType (instance, schema, options, ctx) {
var types = Array.isArray(schema.type) ? schema.type : [schema.type];
if (!types.some(this.testType.bind(this, instance, schema, options, ctx))) {
var list = types.map(function (v) {
if(!v) return;
var id = v.$id || v.id;
return id ? ('<' + id + '>') : (v+'');
return v.id && ('<' + v.id + '>') || (v+'');
});
result.addError({
name: 'type',
@ -62,12 +60,9 @@ validators.type = function validateType (instance, schema, options, ctx) {
function testSchemaNoThrow(instance, options, ctx, callback, schema){
var throwError = options.throwError;
var throwAll = options.throwAll;
options.throwError = false;
options.throwAll = false;
var res = this.validateSchema(instance, schema, options, ctx);
options.throwError = throwError;
options.throwAll = throwAll;
if (!res.valid && callback instanceof Function) {
callback(res);
@ -96,11 +91,9 @@ validators.anyOf = function validateAnyOf (instance, schema, options, ctx) {
if (!schema.anyOf.some(
testSchemaNoThrow.bind(
this, instance, options, ctx, function(res){inner.importErrors(res);}
))) {
))) {
var list = schema.anyOf.map(function (v, i) {
var id = v.$id || v.id;
if(id) return '<' + id + '>';
return(v.title && JSON.stringify(v.title)) || (v['$ref'] && ('<' + v['$ref'] + '>')) || '[subschema '+i+']';
return (v.id && ('<' + v.id + '>')) || (v.title && JSON.stringify(v.title)) || (v['$ref'] && ('<' + v['$ref'] + '>')) || '[subschema '+i+']';
});
if (options.nestedErrors) {
result.importErrors(inner);
@ -135,8 +128,7 @@ validators.allOf = function validateAllOf (instance, schema, options, ctx) {
schema.allOf.forEach(function(v, i){
var valid = self.validateSchema(instance, v, options, ctx);
if(!valid.valid){
var id = v.$id || v.id;
var msg = id || (v.title && JSON.stringify(v.title)) || (v['$ref'] && ('<' + v['$ref'] + '>')) || '[subschema '+i+']';
var msg = (v.id && ('<' + v.id + '>')) || (v.title && JSON.stringify(v.title)) || (v['$ref'] && ('<' + v['$ref'] + '>')) || '[subschema '+i+']';
result.addError({
name: 'allOf',
argument: { id: msg, length: valid.errors.length, valid: valid },
@ -169,10 +161,9 @@ validators.oneOf = function validateOneOf (instance, schema, options, ctx) {
var count = schema.oneOf.filter(
testSchemaNoThrow.bind(
this, instance, options, ctx, function(res) {inner.importErrors(res);}
) ).length;
) ).length;
var list = schema.oneOf.map(function (v, i) {
var id = v.$id || v.id;
return id || (v.title && JSON.stringify(v.title)) || (v['$ref'] && ('<' + v['$ref'] + '>')) || '[subschema '+i+']';
return (v.id && ('<' + v.id + '>')) || (v.title && JSON.stringify(v.title)) || (v['$ref'] && ('<' + v['$ref'] + '>')) || '[subschema '+i+']';
});
if (count!==1) {
if (options.nestedErrors) {
@ -187,70 +178,6 @@ validators.oneOf = function validateOneOf (instance, schema, options, ctx) {
return result;
};
/**
* Validates "then" or "else" depending on the result of validating "if"
* @param instance
* @param schema
* @param options
* @param ctx
* @return {String|null}
*/
validators.if = function validateIf (instance, schema, options, ctx) {
// Ignore undefined instances
if (instance === undefined) return null;
if (!helpers.isSchema(schema.if)) throw new Error('Expected "if" keyword to be a schema');
var ifValid = testSchemaNoThrow.call(this, instance, options, ctx, null, schema.if);
var result = new ValidatorResult(instance, schema, options, ctx);
var res;
if(ifValid){
if (schema.then === undefined) return;
if (!helpers.isSchema(schema.then)) throw new Error('Expected "then" keyword to be a schema');
res = this.validateSchema(instance, schema.then, options, ctx.makeChild(schema.then));
result.importErrors(res);
}else{
if (schema.else === undefined) return;
if (!helpers.isSchema(schema.else)) throw new Error('Expected "else" keyword to be a schema');
res = this.validateSchema(instance, schema.else, options, ctx.makeChild(schema.else));
result.importErrors(res);
}
return result;
};
function getEnumerableProperty(object, key){
// Determine if `key` shows up in `for(var key in object)`
// First test Object.hasOwnProperty.call as an optimization: that guarantees it does
if(Object.hasOwnProperty.call(object, key)) return object[key];
// Test `key in object` as an optimization; false means it won't
if(!(key in object)) return;
while( (object = Object.getPrototypeOf(object)) ){
if(Object.propertyIsEnumerable.call(object, key)) return object[key];
}
}
/**
* Validates propertyNames
* @param instance
* @param schema
* @param options
* @param ctx
* @return {String|null|ValidatorResult}
*/
validators.propertyNames = function validatePropertyNames (instance, schema, options, ctx) {
if(!this.types.object(instance)) return;
var result = new ValidatorResult(instance, schema, options, ctx);
var subschema = schema.propertyNames!==undefined ? schema.propertyNames : {};
if(!helpers.isSchema(subschema)) throw new SchemaError('Expected "propertyNames" to be a schema (object or boolean)');
for (var property in instance) {
if(getEnumerableProperty(instance, property) !== undefined){
var res = this.validateSchema(property, subschema, options, ctx.makeChild(subschema));
result.importErrors(res);
}
}
return result;
};
/**
* Validates properties
* @param instance
@ -264,17 +191,12 @@ validators.properties = function validateProperties (instance, schema, options,
var result = new ValidatorResult(instance, schema, options, ctx);
var properties = schema.properties || {};
for (var property in properties) {
var subschema = properties[property];
if(subschema===undefined){
continue;
}else if(subschema===null){
throw new SchemaError('Unexpected null, expected schema in "properties"');
}
if (typeof options.preValidateProperty == 'function') {
options.preValidateProperty(instance, property, subschema, options, ctx);
options.preValidateProperty(instance, property, properties[property], options, ctx);
}
var prop = getEnumerableProperty(instance, property);
var res = this.validateSchema(prop, subschema, options, ctx.makeChild(subschema, property));
var prop = Object.hasOwnProperty.call(instance, property) ? instance[property] : undefined;
var res = this.validateSchema(prop, properties[property], options, ctx.makeChild(properties[property], property));
if(res.instance !== result.instance[property]) result.instance[property] = res.instance;
result.importErrors(res);
}
@ -284,7 +206,7 @@ validators.properties = function validateProperties (instance, schema, options,
/**
* Test a specific property within in instance against the additionalProperties schema attribute
* This ignores properties with definitions in the properties schema attribute, but no other attributes.
* If too many more types of property-existence tests pop up they may need their own class of tests (like `type` has)
* If too many more types of property-existance tests pop up they may need their own class of tests (like `type` has)
* @private
* @return {boolean}
*/
@ -297,7 +219,7 @@ function testAdditionalProperty (instance, schema, options, ctx, property, resul
result.addError({
name: 'additionalProperties',
argument: property,
message: "is not allowed to have the additional property " + JSON.stringify(property),
message: "additionalProperty " + JSON.stringify(property) + " exists in instance when not allowed",
});
} else {
var additionalProperties = schema.additionalProperties || {};
@ -328,29 +250,17 @@ validators.patternProperties = function validatePatternProperties (instance, sch
for (var property in instance) {
var test = true;
for (var pattern in patternProperties) {
var subschema = patternProperties[pattern];
if(subschema===undefined){
continue;
}else if(subschema===null){
throw new SchemaError('Unexpected null, expected schema in "patternProperties"');
}
try {
var regexp = new RegExp(pattern, 'u');
} catch(_e) {
// In the event the stricter handling causes an error, fall back on the forgiving handling
// DEPRECATED
regexp = new RegExp(pattern);
}
if (!regexp.test(property)) {
var expr = new RegExp(pattern);
if (!expr.test(property)) {
continue;
}
test = false;
if (typeof options.preValidateProperty == 'function') {
options.preValidateProperty(instance, property, subschema, options, ctx);
options.preValidateProperty(instance, property, patternProperties[pattern], options, ctx);
}
var res = this.validateSchema(instance[property], subschema, options, ctx.makeChild(subschema, property));
var res = this.validateSchema(instance[property], patternProperties[pattern], options, ctx.makeChild(patternProperties[pattern], property));
if(res.instance !== result.instance[property]) result.instance[property] = res.instance;
result.importErrors(res);
}
@ -398,7 +308,7 @@ validators.minProperties = function validateMinProperties (instance, schema, opt
name: 'minProperties',
argument: schema.minProperties,
message: "does not meet minimum property length of " + schema.minProperties,
});
})
}
return result;
};
@ -465,22 +375,18 @@ validators.items = function validateItems (instance, schema, options, ctx) {
validators.minimum = function validateMinimum (instance, schema, options, ctx) {
if (!this.types.number(instance)) return;
var result = new ValidatorResult(instance, schema, options, ctx);
var valid = true;
if (schema.exclusiveMinimum && schema.exclusiveMinimum === true) {
if(!(instance > schema.minimum)){
result.addError({
name: 'minimum',
argument: schema.minimum,
message: "must be greater than " + schema.minimum,
});
}
valid = instance > schema.minimum;
} else {
if(!(instance >= schema.minimum)){
result.addError({
name: 'minimum',
argument: schema.minimum,
message: "must be greater than or equal to " + schema.minimum,
});
}
valid = instance >= schema.minimum;
}
if (!valid) {
result.addError({
name: 'minimum',
argument: schema.minimum,
message: "must have a minimum value of " + schema.minimum,
});
}
return result;
};
@ -494,65 +400,17 @@ validators.minimum = function validateMinimum (instance, schema, options, ctx) {
validators.maximum = function validateMaximum (instance, schema, options, ctx) {
if (!this.types.number(instance)) return;
var result = new ValidatorResult(instance, schema, options, ctx);
var valid;
if (schema.exclusiveMaximum && schema.exclusiveMaximum === true) {
if(!(instance < schema.maximum)){
result.addError({
name: 'maximum',
argument: schema.maximum,
message: "must be less than " + schema.maximum,
});
}
valid = instance < schema.maximum;
} else {
if(!(instance <= schema.maximum)){
result.addError({
name: 'maximum',
argument: schema.maximum,
message: "must be less than or equal to " + schema.maximum,
});
}
valid = instance <= schema.maximum;
}
return result;
};
/**
* Validates the number form of exclusiveMinimum when the type of the instance value is a number.
* @param instance
* @param schema
* @return {String|null}
*/
validators.exclusiveMinimum = function validateExclusiveMinimum (instance, schema, options, ctx) {
// Support the boolean form of exclusiveMinimum, which is handled by the "minimum" keyword.
if(typeof schema.exclusiveMaximum === 'boolean') return;
if (!this.types.number(instance)) return;
var result = new ValidatorResult(instance, schema, options, ctx);
var valid = instance > schema.exclusiveMinimum;
if (!valid) {
result.addError({
name: 'exclusiveMinimum',
argument: schema.exclusiveMinimum,
message: "must be strictly greater than " + schema.exclusiveMinimum,
});
}
return result;
};
/**
* Validates the number form of exclusiveMaximum when the type of the instance value is a number.
* @param instance
* @param schema
* @return {String|null}
*/
validators.exclusiveMaximum = function validateExclusiveMaximum (instance, schema, options, ctx) {
// Support the boolean form of exclusiveMaximum, which is handled by the "maximum" keyword.
if(typeof schema.exclusiveMaximum === 'boolean') return;
if (!this.types.number(instance)) return;
var result = new ValidatorResult(instance, schema, options, ctx);
var valid = instance < schema.exclusiveMaximum;
if (!valid) {
result.addError({
name: 'exclusiveMaximum',
argument: schema.exclusiveMaximum,
message: "must be strictly less than " + schema.exclusiveMaximum,
name: 'maximum',
argument: schema.maximum,
message: "must have a maximum value of " + schema.maximum,
});
}
return result;
@ -586,7 +444,7 @@ var validateMultipleOfOrDivisbleBy = function validateMultipleOfOrDivisbleBy (in
result.addError({
name: validationType,
argument: validationArgument,
message: errorMessage + JSON.stringify(validationArgument),
message: errorMessage + JSON.stringify(validationArgument)
});
}
@ -600,7 +458,7 @@ var validateMultipleOfOrDivisbleBy = function validateMultipleOfOrDivisbleBy (in
* @return {String|null}
*/
validators.multipleOf = function validateMultipleOf (instance, schema, options, ctx) {
return validateMultipleOfOrDivisbleBy.call(this, instance, schema, options, ctx, "multipleOf", "is not a multiple of (divisible by) ");
return validateMultipleOfOrDivisbleBy.call(this, instance, schema, options, ctx, "multipleOf", "is not a multiple of (divisible by) ");
};
/**
@ -622,14 +480,14 @@ validators.divisibleBy = function validateDivisibleBy (instance, schema, options
validators.required = function validateRequired (instance, schema, options, ctx) {
var result = new ValidatorResult(instance, schema, options, ctx);
if (instance === undefined && schema.required === true) {
// A boolean form is implemented for reverse-compatibility with schemas written against older drafts
// A boolean form is implemented for reverse-compatability with schemas written against older drafts
result.addError({
name: 'required',
message: "is required",
message: "is required"
});
} else if (this.types.object(instance) && Array.isArray(schema.required)) {
schema.required.forEach(function(n){
if(getEnumerableProperty(instance, n)===undefined){
if(instance[n]===undefined){
result.addError({
name: 'required',
argument: n,
@ -650,15 +508,7 @@ validators.required = function validateRequired (instance, schema, options, ctx)
validators.pattern = function validatePattern (instance, schema, options, ctx) {
if (!this.types.string(instance)) return;
var result = new ValidatorResult(instance, schema, options, ctx);
var pattern = schema.pattern;
try {
var regexp = new RegExp(pattern, 'u');
} catch(_e) {
// In the event the stricter handling causes an error, fall back on the forgiving handling
// DEPRECATED
regexp = new RegExp(pattern);
}
if (!instance.match(regexp)) {
if (!instance.match(schema.pattern)) {
result.addError({
name: 'pattern',
argument: schema.pattern,
@ -783,6 +633,32 @@ validators.maxItems = function validateMaxItems (instance, schema, options, ctx)
return result;
};
/**
* Validates that every item in an instance array is unique, when instance is an array
* @param instance
* @param schema
* @param options
* @param ctx
* @return {String|null|ValidatorResult}
*/
validators.uniqueItems = function validateUniqueItems (instance, schema, options, ctx) {
if (!this.types.array(instance)) return;
var result = new ValidatorResult(instance, schema, options, ctx);
function testArrays (v, i, a) {
for (var j = i + 1; j < a.length; j++) if (helpers.deepCompareStrict(v, a[j])) {
return false;
}
return true;
}
if (!instance.every(testArrays)) {
result.addError({
name: 'uniqueItems',
message: "contains duplicate item",
});
}
return result;
};
/**
* Deep compares arrays for duplicates
* @param v
@ -807,7 +683,6 @@ function testArrays (v, i, a) {
* @return {String|null}
*/
validators.uniqueItems = function validateUniqueItems (instance, schema, options, ctx) {
if (schema.uniqueItems!==true) return;
if (!this.types.array(instance)) return;
var result = new ValidatorResult(instance, schema, options, ctx);
if (!instance.every(testArrays)) {
@ -931,8 +806,7 @@ validators.not = validators.disallow = function validateNot (instance, schema, o
if(!Array.isArray(notTypes)) notTypes=[notTypes];
notTypes.forEach(function (type) {
if (self.testType(instance, schema, options, ctx, type)) {
var id = type && (type.$id || type.id);
var schemaId = id || type;
var schemaId = type && type.id && ('<' + type.id + '>') || type;
result.addError({
name: 'not',
argument: schemaId,