You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Data (please make it as small as possible to reproduce the issue):
"101"
What results did you expect?
This schema is attempting to validate that a string is within the range from 0 to 100, after it is converted to a number. In this case, because the numeric value of the string is greater than 100, it should create an error.
The coerceTypes config property will cause other errors in my schema (not shown in the example) where it should not automatically coerce the type. I need the ability to coerce individual properties in a schema from strings to numbers, without enabling global coercion.
I looked at the addKeyword docs, but it wasn't immediately clear how to change a value and then validate that value as if it was a different type. I can continue to work on this problem if a little guidance is provided.
The text was updated successfully, but these errors were encountered:
Duplicate of #141 (there is a working implementation of keywords modifying the data). Also please read the docs about creating custom keywords (it explains how to modify data too among other things) and see examples of custom keywords in ajv-keywords, particularly dynamicDefaults (it modifies the data).
Thanks @epoberezkin, reading the docs were more clear after a good nights sleep.
For the record, this is what I created:
/** * Coerce a string to another data type and validate as if it was that type * * Useful for numeric values that are transmitted as strings. Since HTTP is * text based, all params, query and header properties are strings. * * Example: * { * "type": "string", * "coerce": { * "type": "number", * "minimum": 0, * "maximum": 22 * } * } */validator.addKeyword('coerce',{type: 'string',compile: function(schema){varvalidateCoerced=validator.compile(schema);returnfunction(data){if(schema.type==='number'||schema.type==='integer'){data=parseFloat(data);}else{thrownewError(`Unable to coerce 'string' to '${schema.type}'`);}returnvalidateCoerced(data);};}});
JSON Schema (please make it as small as possible to reproduce the issue):
Data (please make it as small as possible to reproduce the issue):
"101"
What results did you expect?
This schema is attempting to validate that a string is within the range from 0 to 100, after it is converted to a number. In this case, because the numeric value of the string is greater than 100, it should create an error.
The
coerceTypes
config property will cause other errors in my schema (not shown in the example) where it should not automatically coerce the type. I need the ability to coerce individual properties in a schema from strings to numbers, without enabling global coercion.I looked at the
addKeyword
docs, but it wasn't immediately clear how to change a value and then validate that value as if it was a different type. I can continue to work on this problem if a little guidance is provided.The text was updated successfully, but these errors were encountered: