Commit cb7128e7 authored by Boris Kocherov's avatar Boris Kocherov

tv4: add const support

parent 07dd0ad7
...@@ -725,6 +725,9 @@ ValidatorContext.prototype.validateBasic = function validateBasic(data, schema, ...@@ -725,6 +725,9 @@ ValidatorContext.prototype.validateBasic = function validateBasic(data, schema,
if (error = this.validateType(data, schema, dataPointerPath)) { if (error = this.validateType(data, schema, dataPointerPath)) {
return error.prefixWith(null, "type"); return error.prefixWith(null, "type");
} }
if (error = this.validateConst(data, schema, dataPointerPath)) {
return error.prefixWith(null, "const");
}
if (error = this.validateEnum(data, schema, dataPointerPath)) { if (error = this.validateEnum(data, schema, dataPointerPath)) {
return error.prefixWith(null, "type"); return error.prefixWith(null, "type");
} }
...@@ -755,6 +758,14 @@ ValidatorContext.prototype.validateType = function validateType(data, schema) { ...@@ -755,6 +758,14 @@ ValidatorContext.prototype.validateType = function validateType(data, schema) {
return this.createError(ErrorCodes.INVALID_TYPE, {type: dataType, expected: allowedTypes.join("/")}, '', '', null, data, schema); return this.createError(ErrorCodes.INVALID_TYPE, {type: dataType, expected: allowedTypes.join("/")}, '', '', null, data, schema);
}; };
ValidatorContext.prototype.validateConst = function validateConst(data, schema) {
if (schema.const === undefined ||
recursiveCompare(data, schema.const)) {
return null;
}
return this.createError(ErrorCodes.CONST_NOT_EQUAL, {}, '', '', null, data, schema);
};
ValidatorContext.prototype.validateEnum = function validateEnum(data, schema) { ValidatorContext.prototype.validateEnum = function validateEnum(data, schema) {
if (schema["enum"] === undefined) { if (schema["enum"] === undefined) {
return null; return null;
...@@ -1379,6 +1390,7 @@ var ErrorCodes = { ...@@ -1379,6 +1390,7 @@ var ErrorCodes = {
ONE_OF_MULTIPLE: 12, ONE_OF_MULTIPLE: 12,
NOT_PASSED: 13, NOT_PASSED: 13,
BOOLEAN_SCHEMA_FALSE: 14, BOOLEAN_SCHEMA_FALSE: 14,
CONST_NOT_EQUAL: 15,
// Numeric errors // Numeric errors
NUMBER_MULTIPLE_OF: 100, NUMBER_MULTIPLE_OF: 100,
NUMBER_MINIMUM: 101, NUMBER_MINIMUM: 101,
...@@ -1421,6 +1433,7 @@ var ErrorMessagesDefault = { ...@@ -1421,6 +1433,7 @@ var ErrorMessagesDefault = {
ONE_OF_MULTIPLE: "Data is valid against more than one schema from \"oneOf\": indices {index1} and {index2}", ONE_OF_MULTIPLE: "Data is valid against more than one schema from \"oneOf\": indices {index1} and {index2}",
NOT_PASSED: "Data matches schema from \"not\"", NOT_PASSED: "Data matches schema from \"not\"",
BOOLEAN_SCHEMA_FALSE: "Schema does not allow any data", BOOLEAN_SCHEMA_FALSE: "Schema does not allow any data",
CONST_NOT_EQUAL: "Data does not match schema.const",
// Numeric errors // Numeric errors
NUMBER_MULTIPLE_OF: "Value {value} is not a multiple of {multipleOf}", NUMBER_MULTIPLE_OF: "Value {value} is not a multiple of {multipleOf}",
NUMBER_MINIMUM: "Value {value} is less than minimum {minimum}", NUMBER_MINIMUM: "Value {value} is less than minimum {minimum}",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment