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

fix(schema-extraction): check for _required field in validation rules #6151

Conversation

sgulseth
Copy link
Member

@sgulseth sgulseth commented Mar 29, 2024

Description

We were missing one check when checking for if a field is required.

The reason we were not catching this in our tests was because we have a "lazy copy" of createSchema to avoid depending on sanity/core. The createSchema in sanity/core also infers some validation logic, which our code doens't do.

note: The tsdoc marks the _required field as deprecated, but it isn't deprecated.

What to review

Is this correct? Are we missing any other checks?

Testing

Any other test cases we are missing?

Notes for release

Schema extraction: fixes a bug where we don't mark fields as required

fixes #6150

Copy link

vercel bot commented Mar 29, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
performance-studio ✅ Ready (Inspect) Visit Preview Apr 1, 2024 9:06pm
test-studio ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 1, 2024 9:06pm
1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
studio-workshop ⬜️ Ignored (Inspect) Visit Preview Apr 1, 2024 9:06pm

Copy link
Member Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @sgulseth and the rest of your teammates on Graphite Graphite

Copy link
Contributor

No changes to documentation

@sgulseth sgulseth marked this pull request as ready for review March 29, 2024 11:46
@sgulseth sgulseth requested a review from a team as a code owner March 29, 2024 11:46
@sgulseth sgulseth requested review from ricokahler and removed request for a team March 29, 2024 11:46
Copy link
Contributor

github-actions bot commented Mar 29, 2024

Component Testing Report Updated Apr 1, 2024 9:10 PM (UTC)

File Status Duration Passed Skipped Failed
comments/CommentInput.spec.tsx ✅ Passed (Inspect) 32s 15 0 0
formBuilder/ArrayInput.spec.tsx ✅ Passed (Inspect) 6s 3 0 0
formBuilder/inputs/PortableText/Annotations.spec.tsx ✅ Passed (Inspect) 12s 3 0 0
formBuilder/inputs/PortableText/copyPaste/CopyPaste.spec.tsx ✅ Passed (Inspect) 12s 4 2 0
formBuilder/inputs/PortableText/Decorators.spec.tsx ✅ Passed (Inspect) 12s 6 0 0
formBuilder/inputs/PortableText/FocusTracking.spec.tsx ✅ Passed (Inspect) 32s 15 0 0
formBuilder/inputs/PortableText/Input.spec.tsx ✅ Passed (Inspect) 1m 0s 15 0 0
formBuilder/inputs/PortableText/ObjectBlock.spec.tsx ✅ Passed (Inspect) 1m 2s 18 0 0
formBuilder/inputs/PortableText/RangeDecoration.spec.tsx ✅ Passed (Inspect) 12s 6 0 0
formBuilder/inputs/PortableText/Styles.spec.tsx ✅ Passed (Inspect) 13s 6 0 0
formBuilder/inputs/PortableText/Toolbar.spec.tsx ✅ Passed (Inspect) 20s 9 0 0

@sgulseth sgulseth requested a review from a team as a code owner March 29, 2024 15:33
@sgulseth sgulseth requested review from a team and skogsmaskin and removed request for a team March 29, 2024 15:33
@sgulseth sgulseth force-pushed the 03-29-fix_schema-extraction_check_for__required_field_in_validation_rules branch from b04c584 to aead6d9 Compare March 29, 2024 18:02
Copy link
Member

@rexxars rexxars left a comment

Choose a reason for hiding this comment

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

Looks good. Added a test case for the case where the _required attribute supposedly can be 'optional' - code does the correct check, but just to prevent any regressions as this seems somewhat weird to me.

There is an additional case that would probably handle; on image/file fields, there is an assetRequired() rule which makes it so the asset field is required. This is a bit confusing, but if you have an image/file field that defines additional fields, setting any of those fields would make the base image/file field have a value, and would thus get treated as valid. What you usually want is assetRequired(), which ensures you can't publish a document without an asset for a given field. This weirdness is due to backwards compatibility, and hopefully something we can get rid of in the future.

I am going to merge this as-is for the next release, but might be worth trying to get a separate fix for the assetRequired case.

@rexxars rexxars added this pull request to the merge queue Apr 1, 2024
Merged via the queue into next with commit 5ba26a5 Apr 1, 2024
29 of 33 checks passed
@rexxars rexxars deleted the 03-29-fix_schema-extraction_check_for__required_field_in_validation_rules branch April 1, 2024 21:07
kmelve pushed a commit that referenced this pull request Apr 5, 2024
…#6151)

* fix(schema-extraction): check for _required field in validation rules

* test(schema): add test case for extraction on explicit optional field

---------

Co-authored-by: Espen Hovlandsdal <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Running sanity schema extract --enforce-required-fields doesn't take validation into account.
2 participants