Comments (3)
I think approach 2 is ok, why is it a workaround? "required" keyword validates an object, not a property, so dataPath is correct.
from ajv-errors.
Yes, "required" keyword validates object but "errorMessage.required" allows to have different messages for separate "missing" properties. As you can see from my example there is no direct way to know which message (in error object array) meant for which property within this object. In my projects I add property name to the start of error message to make error processing simpler. It would be more robust solution to receive missing property name from validator.
Simple use case would be:
user fills-in form and posts data to server;
server validates and returns errors with missing required fields;
client application highlights corresponding input fields and shows corresponding message;
Now there is no easy way for client to determine which exact fields were missing and which error message should be shown for which input field.
from ajv-errors.
I have the same need and I have developed the beginning of a function that allows to return an array of objects of type :
[
{
"field": "email",
"message": "email must be a string and is required"
},
{
"field": the name of the field,
"message": message error
},
]
function ajvErrorsToArray(ajvErrors: Array<{}>) {
let errors: Array<{}> = [];
ajvErrors.forEach((error: any) => {
let e: { field: string | null; message: string | null } = {
field: null,
message: null,
};
if (error.instancePath === "") {
error.params.errors.forEach((paramsError: any) => {
if (paramsError.keyword === "required") {
if (error.message) {
e.field = paramsError.params.missingProperty;
e.message = error.message;
} else {
e.field = paramsError.params.missingProperty;
e.message = paramsError.message;
}
}
});
} else {
e.field = error.instancePath.substring(1);
e.message = error.instancePath.substring(1) + " " + error.message;
}
errors.push(e);
});
return errors;
}
from ajv-errors.
Related Issues (20)
- Reference Error with relative JSON pointer HOT 1
- Custom error for minLength on array property is not working HOT 1
- Be able to set custom keyword for error messages HOT 1
- Receiving error that a dependency is missing after installing ajv-errors.
- Create different error for each validation per attribute HOT 3
- `JSONSchemaType` + `anyOf` corresponding to union over objects cannot correctly determine missing fields HOT 2
- Getting instancePath when using with ajv-keywords and uniqueItemProperties HOT 1
- errorMessage not being set on array of objects? HOT 1
- Keyword error replacement in nested objects it's not working
- Required Keyword Error Message - Error is raised at incorrect level HOT 3
- Question: use enum values in custom error message
- Not getting custom error message for "pattern" keyword
- Is it possible to define a "dictionary" of common error messages?
- errorMessage is not a standard keyword in the JSON Schema specification, hence linting fails HOT 1
- Is this project still being maintained?
- Not working in browser
- Issue when compiling standalone validator with ajv-errors and errorMessage:{ properties:{ HOT 1
- When using ajv-errors package I get this error "Cannot read property 'allErrors' of undefined" HOT 1
- is it possible to add identifier in error, to classify errors
- Generates invalid functions? HOT 3
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 ajv-errors.