Skip to content
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

#3059: switch RJSF validator to cfworker/jsonschema #7081

Merged
merged 20 commits into from
Dec 21, 2023

Conversation

twschiller
Copy link
Contributor

@twschiller twschiller commented Dec 9, 2023

What does this PR do?

Remaining Work

  • Finish parity with AJV6 (see failing validator tests)
    • Fix handling of rootSchema in isValid -- I don't understand the intended behavior yet
    • cfworker/jsonschema reports some extra error paths. Need to determine if that's OK in the context of RJSF or if they need to get filtered out
    • Decide if we want to rewrite messages for required properties
      • After: #/required Instance does not have required property \"pass2\".
      • Before: .pass2 is a required property
    • Decide if we want to rewrite message for type errors
      • After: Instance type \"number\" is invalid. Expected \"string\"
  • Figure out why if you mark a dropdown field as required when E2E via the Page Editor, the form can be submitted without selecting something from the dropdown (that could be a Form Builder issue, e.g., it's not marking the properly as required properly)

Reviewer Tips

  • GitHub might not show the validator test file because it's long. I copied it from the RJSF test suite for AJV6 so we can benchmark cfworker/jsonschema

Discussion

  • Testing in E2E is sort of tricky because the HTML widgets enforce format before submission (e.g., for email)
  • I decided to use AJV6 as the baseline even though it's deprecated in order to reduce the behavioral changes. We might want to use AJV8 instead
  • RJSF Validator type uses any a lot. So we end up needing to use it. (I think that's OK, as the validator has full test coverage)

Demo

Note: Under normal circumstances, the html5 validation will display errors instead of RJSF. I disabled the html5 validation to fallback to the schema validation

image image image image

Future Work

  • Upstream to RJSF or put as a separate NPM package

Checklist

  • Add tests
  • New files added to src/tsconfig.strictNullChecks.json (if possible)
  • Designate a primary reviewer

@twschiller twschiller changed the title #3059: [WIP] switch form validator to cfworker #3059: [WIP] switch form validator to cfworker/jsonschema Dec 9, 2023
@twschiller twschiller added the mv3 label Dec 9, 2023
@twschiller twschiller changed the title #3059: [WIP] switch form validator to cfworker/jsonschema #3059: [WIP] switch RJSF validator to cfworker/jsonschema Dec 9, 2023
Copy link

codecov bot commented Dec 15, 2023

Codecov Report

Attention: 9 lines in your changes are missing coverage. Please review.

Comparison is base (933427f) 71.14% compared to head (579084f) 71.18%.
Report is 1 commits behind head on main.

Files Patch % Lines
src/validators/formValidator.ts 87.32% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #7081      +/-   ##
==========================================
+ Coverage   71.14%   71.18%   +0.03%     
==========================================
  Files        1213     1214       +1     
  Lines       37613    37687      +74     
  Branches     7075     7088      +13     
==========================================
+ Hits        26760    26826      +66     
- Misses      10853    10861       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@grahamlangford grahamlangford changed the title #3059: [WIP] switch RJSF validator to cfworker/jsonschema #3059: switch RJSF validator to cfworker/jsonschema Dec 19, 2023
// Deep clone the schema because otherwise the schema is not extensible
// This breaks validation when @cfworker/json-schema dereferences the schema
// See https://github.com/cfworker/cfworker/blob/263260ea661b6f8388116db7b8daa859e0d28b25/packages/json-schema/src/dereference.ts#L115
schema={cloneDeep(schema)}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Does the cloned schema need to be memoized?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I started off doing that, but I haven't had any issues with performance or mounting/unmounting. I know with formik and react-hook-form, the component won't remount/reset unless the key changes. I think we have the same situation here.

@twschiller
Copy link
Contributor Author

image

Copy link
Contributor

@BLoe BLoe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would like to see some cleanup of the comments in the isValid function, do we need to make further changes in this PR? Are the comments needed for the future? Just seems a bit uncertain what's going on there, when reading the code/comments from an outside perspective.

src/bricks/transformers/ephemeralForm/EphemeralForm.tsx Outdated Show resolved Hide resolved
src/components/formBuilder/preview/FormPreview.tsx Outdated Show resolved Hide resolved
Comment on lines 192 to 206
// XXX: technically we may want to read from $schema to determine which draft version to validate against
// Draft 7 is the default for RJSF: https://rjsf-team.github.io/react-jsonschema-form/docs/usage/validation/#custom-meta-schema-validation

if (!this.isValidSchema(schema)) {
return false;
}

// FIXME: figure out how to add the root schema to the validator
const validator = new Validator(
withIdRefPrefix(schema) as Schema,
// The default used by RJSF: https://rjsf-team.github.io/react-jsonschema-form/docs/usage/validation/#custom-meta-schema-validation
"7",
true,
);
// This breaks due to duplicate ids: validator.addSchema(rootSchema as Schema);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything that needs to be done here with this series of XXX / FIXME / "This breaks" comments? Do we need to leave all of these in here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one here is the main reason I didn't approve the PR yet, fyi

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch - this is a feature that I'm not sure if we need or not, or even what the feature does

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At one point I confirmed that this is called, but I'm still unclear what it's doing. Every test case I could come up with is passing. I'll remove the comment.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

grahamlangford and others added 2 commits December 20, 2023 15:31
Co-authored-by: Ben Loe <below413@gmail.com>
Co-authored-by: Ben Loe <below413@gmail.com>
Copy link

No loom links were found in the first post. Please add one there if you'd like to it to appear on Slack.

Do not edit this comment manually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

Support RJSF Schema validation in strict CSP
4 participants