tv4 icon indicating copy to clipboard operation
tv4 copied to clipboard

default value

Open conradoqg opened this issue 11 years ago • 5 comments

I think that defaut value in schema isn't working...

conradoqg avatar Feb 06 '14 02:02 conradoqg

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;
    };

conradoqg avatar Feb 06 '14 02:02 conradoqg

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?

geraintluff avatar Feb 07 '14 17:02 geraintluff

If it's not coerced, what's the use of the default keyword?

vweevers avatar Jul 08 '14 11:07 vweevers

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.

geraintluff avatar Jul 08 '14 13:07 geraintluff

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!

vweevers avatar Jul 08 '14 14:07 vweevers