korzio / djv

Dynamic JSON Schema Validator - Supports draft-04/06
https://www.npmjs.com/package/djv
MIT License
289 stars 31 forks source link

Error messages does not really make any sense #67

Open vladyslav2 opened 6 years ago

vladyslav2 commented 6 years ago

Please do not do same error messages as ajv did, they are not really helpful. I'm moving away from ajv cause it's hard to understand the problem based just on error message. You have to check the source code to get useful info. And whats harder is to create user-friendly error messages. I guess with ajv it's not really possible.

I tested your library, I decide to modify a home page example, simply pass empty object to validation function:

const env = new djv();
const jsonSchema = {
  "common": {
    "properties": {
      "type": {
        "enum": ["common"]
      }
    },
    "required": [
      "type"
    ]
  }
};

// Use `addSchema` to add json-schema
env.addSchema('test', jsonSchema);
env.validate('test#/common', {});

I got in return:

{ keyword: 'required', dataPath: '', schemaPath: '#/required' }

With does not really useful - there is no actual name of the field which is required, which means it's not possible to build user-friendly error either.

But your library looks much easier and flexible then ajv, hope you will fix this issue!

Anthropic commented 6 years ago

@vladyslav2 I think you should comment on this issue 396 in: https://github.com/json-schema-org/json-schema-spec

There is a goal to standardise a suggested implementation for the json-schema error format.

For making switching less painful we want the results to be the same, if a standard is reached then ajv will most likely also update as well.

Yes djv is the best :)

vladyslav2 commented 6 years ago

@Anthropic I'm not sure if they would understand me. I'm from my side cannot understand approach they took.

Why don't you make it in a simple way:

{
   fieldPath: [{
         keyword,
         message,
         params
   }, ... ],
   ...
}

Instead, fieldPath is floating from one parameter to other based on the error.

juanmait commented 4 years ago

I've the same issue. The first time that I try the library.

const schema = {
  creds: {
    required: ['username', 'password'],
    properties: {
      username: {
        type: 'string',
      },
      password: {
        type: 'string',
      },
    },
  },
};

env.addSchema('creds', schema);
env.validate('creds#/creds', {});
// => { keyword: 'required', dataPath: '', schemaPath: '#/required' } 

So there is no way to know what did failed? Ajv have better errors but it has such a bad implementation. The way that it works makes compiling schemas before hand pretty much useless.