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

fails when id in schema is not a string (invalid schema) - improve error reporting #380

Closed
scott-martin opened this issue Dec 24, 2016 · 3 comments

Comments

@scott-martin
Copy link

scott-martin commented Dec 24, 2016

What version of Ajv are you using? Does the issue happen if you use the latest version?
4.10.2

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

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "id": {
      "type": "integer"
    },
    "first_name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 50
    },
    "last_name": {
      "type": "string",
      "minLength": 1,
      "maxLength": 50
    },
    "phone_number": {
      "type": "string",
      "format": "phone"
    },
  },
  "required": [
    "id",
    "first_name",
    "last_name",
    "phone_number"
  ]
}

Data (please make it as small as posssible to reproduce the issue):

{
  "id": 1,
  "first_name": "Draco",
  "last_name": "Malfoy",
  "phone_number": "+18435550001"
}                   

Your code (please use options, schema and data as variables):

'use strict';

const Ajv = require('ajv');
const schema = require('./contact.schema');
let ajv = Ajv({ allErrors: true });
ajv.addFormat('phone', () => { return true; });
ajv.compile(schema);

exports.validate = function (data) {
    ajv.validate(data);
    return ajv.errors;
};

Validation result, data AFTER validation, error messages:

TypeError: id.replace is not a function
    at Function.normalizeId (/XXXX/node_modules/ajv/lib/compile/resolve.js:216:18)
    at _addSchema (/XXXX/node_modules/ajv/lib/ajv.js:282:22)
    at Ajv.validate (/XXXX/node_modules/ajv/lib/ajv.js:94:23)

Currently using integer ids in my project. As you can see, ajv is failing because there is no replace function on the integer type in Javascript.

@epoberezkin
Copy link
Member

@scott-martin please check docs - there are 3 suggested options in Getting started, you are not using it correctly. ajv.validate expects a schema as the first parameter.

@epoberezkin
Copy link
Member

epoberezkin commented Dec 24, 2016

Maybe better error reporting here would be helpful...

@epoberezkin epoberezkin reopened this Dec 24, 2016
@epoberezkin epoberezkin changed the title normalizeId fails when id is not string normalizeId fails when id is not string - improve error reporting Dec 24, 2016
@epoberezkin epoberezkin changed the title normalizeId fails when id is not string - improve error reporting normalizeId fails when id is not string (invalid schema) - improve error reporting Dec 24, 2016
@epoberezkin epoberezkin changed the title normalizeId fails when id is not string (invalid schema) - improve error reporting fails when id in schema is not a string (invalid schema) - improve error reporting Dec 24, 2016
@IBwWG IBwWG mentioned this issue Jan 4, 2017
@epoberezkin epoberezkin added this to the 5.0.0 milestone Feb 4, 2017
@epoberezkin
Copy link
Member

in 5.0.1-beta.3

@epoberezkin epoberezkin added the label Feb 4, 2017
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