djv
djv copied to clipboard
Suggestion: Standardised error messages
Would it be worth considering making a transition from/to ajv easier by allowing djv to return a similar error object and additional API items like addMetaSchema?
@Anthropic Yes, it would be, I agree. It is better to have validators do work in a similar style. I would plan this as an enhancement for the next minor releases.
https://gitter.im/korzio/djv?at=59f1ba7b210ac26920327dfb
I'm working on a schema in which almost every field is a $ref
and so the errors I get back are all $ref: data
which isn't very useful. From AJV I get something like:
dataPath: '.threads[3].thread.message',
schemaPath: '#/required',
params: { missingProperty: 'from_user' },
message: 'should have required property \'from_user\'' } ]```.
Returning errors like AJV does would be really really useful.
@aboyton Yes, I understand. The current approach is just the easiest one. This task is about standard formatted messages and it is planned for the next sprint.
That would be great. Let me know if there’s anything I can do to help. I looked around the code and it wasn’t obvious to me how to easily add it.
@aboyton If you are interested, @Anthropic has been working on a nice validators adapters package and he's already done good work regarding the messages standardisation for djv
library.
👍
Oh I'd love this. I'm trying to write up some example code for a new book, and the examples are failing for me.
https://github.com/apisyouwonthate/talking-to-other-peoples-apis-code/tree/master/json-schema-validation
Clone the whole repo:
cd json-schema-validation
yarn install
node example-user/run.js
undefined:22
data: '['name']'
^^^^
SyntaxError: Unexpected identifier
at new Function (<anonymous>)
at restore (/Users/psturgeon/src/talking-to-other-peoples-apis-code/json-schema-validation/node_modules/djv/lib/utils/template.js:110:15)
at generate (/Users/psturgeon/src/talking-to-other-peoples-apis-code/json-schema-validation/node_modules/djv/lib/utils/state.js:50:10)
at Environment.addSchema (/Users/psturgeon/src/talking-to-other-peoples-apis-code/json-schema-validation/node_modules/djv/lib/djv.js:87:11)
at Object.<anonymous> (/Users/psturgeon/src/talking-to-other-peoples-apis-code/json-schema-validation/example-user/run.js:16:5)
at Module._compile (module.js:612:30)
at Object.Module._extensions..js (module.js:623:10)
at Module.load (module.js:531:32)
at tryModuleLoad (module.js:494:12)
at Function.Module._load (module.js:486:3)
If we cannot get standard errors baked into the library easily soon, could we at least get a working example for robust errors into the README?
Hey @philsturgeon as @korzio mentioned you may find Assimilate can help as it can test with ajv,djv and tv4 currently. You can see how it works in the test: https://github.com/jsonschema/assimilate/blob/master/spec/index.spec.ts
Still more work to do on it, but it is getting at least the basics sorted while hopefully making switching libraries much easier in future (especially when this issue is merged). I'm in the jsonschema org and slack channel with you already, so yell out if you want/need anything related to it :)
Thank you. If it was for an implementation I was working on I'd just use it, but im trying to show off "How to do X" in a book, and I need a solution that'll stick still for a while. I'm using AJV for now sadly, but good luck with this!
If you are looking at adding standardised error messages, I would love to seem them be something you can choose to add to the existing error object and be designed with i18n/l20n in mind. For example using Fluent. I would be happy to start on an PR for this if you'd like?