Comments (4)
There is definitely room for improvements with locales in error handling and it is something i have been "struggling" with when writing the library. The main difficulty is i guess to find a way to handle all errors, those that imply one field, those implying two fields etc... Which might result in dealing with different kinds of structures for each error type.
Also this is going to be some kind of archeology work for now :) Making sure all errors are taken in account ( schema creation ones + validation ones ).
I honestly dont know what is the best solution but here are some thoughts:
- We must keep the standard locales modes, forcing the library user to implements all kinds of errors before being able to use it is quite repulsive.
- Allowing the user to override the locales in a Printf fashion can be a good solution. ex : SetErrorLocale( Type, "%field Must be greater than %value" )
- We could provide interfaces and default implementations for all types of errors. So that the user can use the default ones or write/update his own versions. This may also allow them to write their own formatters, a good thing when dealing with JSON APIs for example.
- If there is a single way to represent all kinds of errors, i mean using the same structure, i guess the Type system works. If not, I am afraid the user will have to perform some big ugly switch to handle all types and output them accordingly.
from gojsonschema.
Thanks for the thoughts. There are definitely several different layers of accomplishing this.
The example I put together is the simplest I could think of: Add a new Type field to the struct and identify the error. Then each developer can work with that in their own library to implement their own responses (should they need to) like the example I gave above. The idea here is to just add more context to the ResultError, which is only used for validation - so I did not do anything with schema errors.
The locales currently can't be overwritten because they are constants... I left that as is.
The user also would not have implement anything to use and this would be backwards compatible. They would just receive an additional Type field on the errors with a string like "invalid_type", "enum", etc. If there are multiple errors, there would be multiple ResultError structs returned, and each of those would have the new Type field so it would still address that use case.
I'm curious to get your thoughts just given the limited scope. What I'm going for is something similar to #6 which just adds more context to the error without breaking BC.
As a more long-term solution, I think allowing library users to create a locale struct or map[string]string and pass that into the library... otherwise the default locale will be used. This would do away with the locale constants. Each error should contain error details that are specific to that error. For example: invalid type might contain details: { "expected": "string", "given": TYPE_BOOLEAN} which can then be converted into the string replacements %expected% and %given%. GTE might contain ErrorDetails{"min": 10}. The locales would then return a string like: "%field% must be a %expected%, %given% given" and the library will handle the replacements. Each error would be it's own type (i.e. InvalidTypeError and still have a type field for easy comparison).
addError would then be called like this:
result.addError(ErrorTypeInvalid, context, currentNode, ErrorDetails{"expected": currentSubSchema.types.String(), "given": TYPE_STRING})
which would return an error struct that looks like this:
InvalidTypeError {
Type: "invalid_type",
Context: *jsonContext,
Description: "firstName should be a string, boolean given"
Value: value,
Details: ErrorDetails{
"expected": "string"
"given": "boolean"
}
}
And as a string:
(root).firstName firstName should be a string, boolean given
Schema errors could be handled the same way, but the call to errors.New() would just be a string that is generated in the same way as validation errors.
from gojsonschema.
I'm actually working through a prototype of the second-half of my last comment. I'll just submit as a PR in the next day or two to get your thoughts.
from gojsonschema.
Just realized I didn't link to this discussion in my PR. Here's the link the PR: #52
from gojsonschema.
Related Issues (20)
- Validation should fail if required key is Go Zero Value for the corresponding type
- gojsonschema.NewGoLoader() returns NOTHING
- validation mishandles anyOf
- Load schema from string HOT 1
- The validate cannot find unknow fields
- Retry on "Could not read schema from HTTP, response status is 503 Service Temporarily Unavailable" HOT 2
- Validate return err on golang 1.19 HOT 1
- A DOS vulnerability exists in parameter validity check. HOT 2
- can we create json schema from json data HOT 1
- Using Frogbot to scan this repository
- Get all properties under a section
- Wrong validation result for nested objects HOT 2
- Usage of gojsonschema in prominent open-source or large-scale projects?
- Validation errors with integers
- Error "Must be a multiple of 0.01" for Value=2000000.13
- Hostname format false positives
- Support `errorMessage` so as to customise validation errors
- major api improvement HOT 2
- invalid character '{{character}}' looking for beginning of value
- Schema validation does not return error for invalid schema definition
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gojsonschema.