-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Bump/ajv #2542
Bump/ajv #2542
Conversation
docs/4.x upgrade guide.md
Outdated
|
||
### JSON Schema draft-04 | ||
|
||
This version drops support for JSON schema draft04. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use "draft-04" and "draft-07"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅
docs/4.x upgrade guide.md
Outdated
|
||
## Breaking changes | ||
|
||
### JSON Schema draft-04 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
explicitly mention that we're bumping ajv to v7, therefore:
- lost support for draft-04
- etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✅
// ajv@7.0^ has changed the format of error.dataPath. In ajv@6.0^ the error | ||
// in the illformed key will result in dataPath = ['bar.\\'"[]()=+*&^%$#@!']. | ||
// However in ajv@7.0 and higher this key results in | ||
// dataPath = bar.'"[]()=+*&^%$#@!. lodash breaks this into error paths |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note to @epicfaace -- look into why this is happening
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@newt10 provide code sample to test this on ajv
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@epicfaace Here is the code sample -> https://runkit.com/embed/qtzmcsm9k62w
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's my own experiment: https://runkit.com/epicfaace/615235af9dcba10008c41a04
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also update https://github.com/rjsf-team/react-jsonschema-form/blob/3ec17f1c0ff40401b7a99c5e9891ac2834a1e73f/docs/usage/validation.md#custom-error-messages to reflect what we actually get (we might get a json pointer instead of a dot notation path)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON pointer spec: https://datatracker.ietf.org/doc/html/rfc6901
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JSON pointer spec: https://datatracker.ietf.org/doc/html/rfc6901
* @param {string} property source of error in the data. | ||
* @returns {stringp[]} array of object paths. | ||
*/ | ||
function genPaths(property) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's replace this with using the json-pointer npm library and use pointer.parse
(https://www.npmjs.com/package/json-pointer)
validationError.message && | ||
typeof validationError.message === "string" && | ||
validationError.message.includes("no schema with key or ref "); | ||
const includeValidationErrors = shouldIncludeSchemaErrors(validationError); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment: #1130 (comment)
Commit: 2e5d5b9
} catch (err) { | ||
validationError = err; | ||
} | ||
|
||
if (validationError === null) { | ||
try { | ||
ajv.validate(schema, formData); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ajv v7 changes:
- If you call
ajv.validate
on an invalid schema twice, the second time it will (incorrectly) not throw any errors due to the schema being invalid.
In general, to note about ajv:
- If you call
ajv.validateSchema
, it usually returns false if the schema is invalid. However, if the meta schema is not found, it will throw an exception.
Goal: Go around ajv v7 changes. Aim for same behavior as before (we want to catch all metaschema errors in validationError
).
Current solution: Call ajv.validateSchema
beforehand, but regardless of whether the schema is valid, call ajv.validate
.
New solution: ???
allErrors: true, | ||
multipleOfPrecision: 8, | ||
schemaId: "auto", | ||
unknownFormats: "ignore", | ||
strictTypes: "log", | ||
}); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest that we allow overriding ajv instance options.
function createAjvInstance(options) { | |
const ajv = new Ajv({ | |
allErrors: true, | |
multipleOfPrecision: 8, | |
strictTypes: "log", | |
...options | |
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That way we can pass the options in the form validation
export default function validateFormData(
formData,
schema,
customValidate,
transformErrors,
additionalMetaSchemas = [],
customFormats = {}
validationOptions={}
) {
// Include form data with undefined values, which is required for validation.
const rootSchema = schema;
formData = getDefaultFormState(schema, formData, rootSchema, true);
const newMetaSchemas = !deepEquals(formerMetaSchema, additionalMetaSchemas);
const newFormats = !deepEquals(formerCustomFormats, customFormats);
if (newMetaSchemas || newFormats|| validationOptions) {
ajv = createAjvInstance(validationOptions);
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allows more control over validation without having to reimplement custom validate function
https://ajv.js.org/options.html
@newt10 any update on this PR? |
Is this PR still maintained by someone ? It would be definitely nice to have AJV v7 here :) |
as work has stalled here, and no work has happened on the ajv 7.x line, It seems like going straight to ajv 8 might make more sense at the next rjsf version bump. https://github.com/ajv-validator/ajv/blob/master/docs/v6-to-v8-migration.md |
Agreed.
…--
Ashwin Ramaswami
On Sat, Feb 5, 2022 at 4:06 PM Nicholas Bollweg ***@***.***> wrote:
as work has stalled here, and no work has happened on the ajv 7.x line, It
seems like going straight to ajv 8 might make more sense at the next rjsf
version bump.
https://github.com/ajv-validator/ajv/blob/master/docs/v6-to-v8-migration.md
—
Reply to this email directly, view it on GitHub
<#2542 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAM4MX2Q6IKBFAAPN4TP6ODUZWGPDANCNFSM5DYJ2MZQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Another way to think about it, might be to have more of an interface-driven design, as with themes, that hid the ajv-specific exports. This would potentially offer some trades between size, start-up time, and live performance, such as:
|
That would indeed be nice! |
I've hoisted some of these ideas over here: #2693 |
@newt10 Closing this as we have extracted the validation code into its own package(s) which is used as a required plug-in to |
Reasons for making this change
AJV@6 is EOL. This PR updates the ajv to v7.
If this is related to existing tickets, include links to them as well. Use the syntax
fixes #2541
.Checklist
Tests
All tests have been updated to work with the new ajv library and all tests are passing
Playground
All playground forms are rendering as expected with the correct response submit.