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

Type of error? #54

Closed
DrakaSAN opened this issue Nov 10, 2016 · 3 comments
Closed

Type of error? #54

DrakaSAN opened this issue Nov 10, 2016 · 3 comments

Comments

@DrakaSAN
Copy link

I like that library a lot, but I have a problem with the error output. While a human readable message is fine when using it for a pet project, it become a pain to use when you have to map it to a error code.

What I mean is, I can easily know programatically which field have a error, but knowing what raised the error would involve parsing the error message, which is a horrible way, since the message could change at any time and break the system.

Would it be possible to add something like "reason" which provide the reason of the error, like "missing field", "wrong type"? I am willing to add it myself, if you could point me to where the error message is generated.

@Atinux
Copy link
Collaborator

Atinux commented Nov 10, 2016

Hi @d219

The error field might to the work :)
https://github.com/Atinux/schema-inspector#error

@Atinux Atinux closed this as completed Nov 10, 2016
@DrakaSAN
Copy link
Author

DrakaSAN commented Nov 10, 2016

This only replace the error message, or I misunderstand the documentation. And it doesn't allow me to, for example, get the real type unless I check manually after getting the error.

@Atinux Atinux reopened this Nov 10, 2016
@Atinux
Copy link
Collaborator

Atinux commented Nov 10, 2016

Sorry I did not read carefully your initial message, it's actual a good idea.

I implemented in the version 1.6.7, you will have a reason key which is the key where the validation failed.

Example:

var schema = {
    type: 'object',
    properties: {
        lorem: { type: 'string', eq: 'ipsum' },
        dolor: {
            type: 'array',
            items: { type: 'number' }
        }
    }
};
var candidate = {
    lorem: 'not_ipsum',
    dolor: [ 12, 34, 'ERROR', 45, 'INVALID' ]
};
var result = inspector.validate(schema, candidate, function (err, result) {
    console.log(result);
});

Result:

{ error: 
   [ { code: null,
       reason: 'eq',
       message: 'must be equal to "ipsum", but is equal to "not_ipsum"',
       property: '@.lorem' },
     { code: null,
       reason: 'type',
       message: 'must be number, but is string',
       property: '@.dolor[2]' },
     { code: null,
       reason: 'type',
       message: 'must be number, but is string',
       property: '@.dolor[4]' } ],
  valid: false,
  format: [Function: format] }

If, in your exec method, you want to give a reason, it's in the 3rd argument in this.report(message, code, reason)

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

No branches or pull requests

2 participants