tv4
tv4 copied to clipboard
default value
I think that defaut value in schema isn't working...
This will resolve the 'default' validation:
ValidatorContext.prototype.validateObjectRequiredProperties = function validateObjectRequiredProperties(data, schema) {
if (schema.required !== undefined) {
for (var i = 0; i < schema.required.length; i++) {
var key = schema.required[i];
if (data[key] === undefined) {
if (schema.properties && schema.properties[key] && schema.properties[key].default) {
data[key] = schema.properties[key].default
} else {
var error = this.createError(ErrorCodes.OBJECT_REQUIRED, {key: key}).prefixWith(null, "" + i).prefixWith(null, "required");
if (this.handleError(error)) {
return error;
}
}
}
}
}
return null;
};
In a normal validation scenario, the data is not modified. Therefore, if a required property isn't present then validation fails.
You seem to be talking about "coercive validation", where the data is altered to fit the schema. Is this a request for coercive validation support?
If it's not coerced, what's the use of the default
keyword?
For non-coercive validation? None, it's informative only, much like "title" or "description".
If you want coercive validation, I started a project tv4-coerce
. It hasn't generated much interest yet, but if you like I can look at putting default
support into it.
Ok, makes sense. I was just curious. For now I only need defaults, so I'll implement it myself; tv4-coerce
is overkill. But I'll keep an eye on it. Thanks!