Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coercing individual properties #399

Closed
scothis opened this issue Jan 24, 2017 · 2 comments
Closed

Coercing individual properties #399

scothis opened this issue Jan 24, 2017 · 2 comments

Comments

@scothis
Copy link

scothis commented Jan 24, 2017

JSON Schema (please make it as small as possible to reproduce the issue):

{ "type": "string", "coerce": { "type": "number", "minimum": 0, "maximum": 100 } }

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.

@epoberezkin
Copy link
Member

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).

@scothis
Copy link
Author

scothis commented Jan 27, 2017

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) {
    var validateCoerced = validator.compile(schema);
    return function (data) {
      if (schema.type === 'number' || schema.type === 'integer') {
        data = parseFloat(data);
      } else {
        throw new Error(`Unable to coerce 'string' to '${schema.type}'`);
      }
      return validateCoerced(data);
    };
  }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants