Improve schema path when using getPropertySchema

This commit is contained in:
toasted-nutbread 2020-02-02 11:18:13 -05:00
parent b1fc9c024a
commit fff1e67a5e

View File

@ -119,7 +119,7 @@ class JsonSchemaProxyHandler {
throw new Error('construct not supported'); throw new Error('construct not supported');
} }
static getPropertySchema(schema, property, value) { static getPropertySchema(schema, property, value, path=null) {
const type = JsonSchemaProxyHandler.getSchemaOrValueType(schema, value); const type = JsonSchemaProxyHandler.getSchemaOrValueType(schema, value);
switch (type) { switch (type) {
case 'object': case 'object':
@ -128,6 +128,7 @@ class JsonSchemaProxyHandler {
if (JsonSchemaProxyHandler.isObject(properties)) { if (JsonSchemaProxyHandler.isObject(properties)) {
const propertySchema = properties[property]; const propertySchema = properties[property];
if (JsonSchemaProxyHandler.isObject(propertySchema)) { if (JsonSchemaProxyHandler.isObject(propertySchema)) {
if (path !== null) { path.push(['properties', properties], [property, propertySchema]); }
return propertySchema; return propertySchema;
} }
} }
@ -136,9 +137,12 @@ class JsonSchemaProxyHandler {
if (additionalProperties === false) { if (additionalProperties === false) {
return null; return null;
} else if (JsonSchemaProxyHandler.isObject(additionalProperties)) { } else if (JsonSchemaProxyHandler.isObject(additionalProperties)) {
if (path !== null) { path.push(['additionalProperties', additionalProperties]); }
return additionalProperties; return additionalProperties;
} else { } else {
return JsonSchemaProxyHandler._unconstrainedSchema; const result = JsonSchemaProxyHandler._unconstrainedSchema;
if (path !== null) { path.push([null, result]); }
return result;
} }
} }
case 'array': case 'array':
@ -151,6 +155,7 @@ class JsonSchemaProxyHandler {
if (property >= 0 && property < items.length) { if (property >= 0 && property < items.length) {
const propertySchema = items[property]; const propertySchema = items[property];
if (JsonSchemaProxyHandler.isObject(propertySchema)) { if (JsonSchemaProxyHandler.isObject(propertySchema)) {
if (path !== null) { path.push(['items', items], [property, propertySchema]); }
return propertySchema; return propertySchema;
} }
} }
@ -160,9 +165,12 @@ class JsonSchemaProxyHandler {
if (additionalItems === false) { if (additionalItems === false) {
return null; return null;
} else if (JsonSchemaProxyHandler.isObject(additionalItems)) { } else if (JsonSchemaProxyHandler.isObject(additionalItems)) {
if (path !== null) { path.push(['additionalItems', additionalItems]); }
return additionalItems; return additionalItems;
} else { } else {
return JsonSchemaProxyHandler._unconstrainedSchema; const result = JsonSchemaProxyHandler._unconstrainedSchema;
if (path !== null) { path.push([null, result]); }
return result;
} }
} }
default: default:
@ -381,18 +389,19 @@ class JsonSchemaProxyHandler {
} }
for (let i = 0, ii = value.length; i < ii; ++i) { for (let i = 0, ii = value.length; i < ii; ++i) {
const propertySchema = JsonSchemaProxyHandler.getPropertySchema(schema, i, value); const schemaPath = [];
const propertySchema = JsonSchemaProxyHandler.getPropertySchema(schema, i, value, schemaPath);
if (propertySchema === null) { if (propertySchema === null) {
throw new JsonSchemaValidationError(`No schema found for array[${i}]`, value, schema, info); throw new JsonSchemaValidationError(`No schema found for array[${i}]`, value, schema, info);
} }
const propertyValue = value[i]; const propertyValue = value[i];
for (const [p, s] of schemaPath) { info.schemaPush(p, s); }
info.valuePush(i, propertyValue); info.valuePush(i, propertyValue);
info.schemaPush(i, propertySchema);
JsonSchemaProxyHandler.validate(propertyValue, propertySchema, info); JsonSchemaProxyHandler.validate(propertyValue, propertySchema, info);
info.schemaPop();
info.valuePop(); info.valuePop();
for (let i = 0; i < schemaPath.length; ++i) { info.schemaPop(); }
} }
} }
@ -419,18 +428,19 @@ class JsonSchemaProxyHandler {
} }
for (const property of properties) { for (const property of properties) {
const propertySchema = JsonSchemaProxyHandler.getPropertySchema(schema, property, value); const schemaPath = [];
const propertySchema = JsonSchemaProxyHandler.getPropertySchema(schema, property, value, schemaPath);
if (propertySchema === null) { if (propertySchema === null) {
throw new JsonSchemaValidationError(`No schema found for ${property}`, value, schema, info); throw new JsonSchemaValidationError(`No schema found for ${property}`, value, schema, info);
} }
const propertyValue = value[property]; const propertyValue = value[property];
for (const [p, s] of schemaPath) { info.schemaPush(p, s); }
info.valuePush(property, propertyValue); info.valuePush(property, propertyValue);
info.schemaPush(property, propertySchema);
JsonSchemaProxyHandler.validate(propertyValue, propertySchema, info); JsonSchemaProxyHandler.validate(propertyValue, propertySchema, info);
info.schemaPop();
info.valuePop(); info.valuePop();
for (let i = 0; i < schemaPath.length; ++i) { info.schemaPop(); }
} }
} }