fix(javascript-prop-types): emit .isRequired for required properties#3032
Open
schani wants to merge 8 commits into
Open
fix(javascript-prop-types): emit .isRequired for required properties#3032schani wants to merge 8 commits into
schani wants to merge 8 commits into
Conversation
…1561) Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
Member
Author
|
I think we should be principled with the tests here and figure out a way to test proptypes so that this case can be surfaced by a fixture test. Why can’t we run schema fixtures for proptypes? |
Generated-output differences215 files differ — 213 modified, 2 new, 0 deleted |
Generated-output differences216 files differ — 214 modified, 2 new, 0 deleted |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Bug
javascript-prop-typesnever emitted.isRequired, even for properties listed in a JSON Schema'srequiredarray. A property like"disabled": { "type": "boolean" }marked required would render asPropTypes.boolinstead ofPropTypes.bool.isRequired.Repro
{ "type": "object", "required": ["disabled"], "properties": { "type": { "type": "string", "enum": ["primary", "secondary"] }, "disabled": { "type": "boolean" } } }Before:
After (matches the issue's expected output):
Root cause
JavaScriptPropTypesRenderer.typeMapTypeFor(t, required = true)had a dead branch —if (required) { return [match]; }just wrapped the value in an array with no effect on the rendered source, so.isRequiredwas never appended.typeMapTypeForPropertyalso always called it with the defaultrequired = trueregardless of the JSON Schema property's actualrequired/isOptionalstatus, so the flag never correctly reflected "is this property required."Fix
typeMapTypeForPropertynow threads through!p.isOptional(and additionally skips.isRequiredwhen the property's type includesnull, sincePropTypes.isRequiredrejects a legitimately-null value for a nullable-but-present property).typeMapTypeFornow actually appends.isRequiredto the generatedPropTypesvalidator whenrequiredis true, for both primitive/array/union validators and named class/object/enum references..isRequiredwas never used:PropTypes.oneOfType(['a', 'b'])(raw literals passed tooneOfType, which expects validator functions) instead ofPropTypes.oneOf(['a', 'b']). This is now fixed;.isRequiredon an enum now works correctly.let _Name;declaration is assigned later in emission order), so accessing.isRequiredon it eagerly could throw at module-eval time. These references are now wrapped in a small lazily-evaluated function so the property access happens at validation time, once the referenced value is guaranteed to be assigned.PropTypes.objectOf(...)instead of an invalid reference to an undeclared_Name.Test coverage
schema-javascript-prop-typesfixture with one valid sample and six expected-failure samples. It exercises requiredany, boolean, enum, nested-object, array, and nested properties while leaving an optional property absent.schema-javascript-prop-typesto the PropTypes CI fixture group. The regression fixture fails against the pre-fix renderer and passes with this change.Verification performed locally
npm run buildnpm run lintnpm run test:unit— 218 tests pass.QUICKTEST=true FIXTURE=javascript-prop-types,schema-javascript-prop-types npm run test:fixtures— 220 fixture cases pass; the schema case validates one positive and six negative documents.Fixes #1561
🤖 Generated with Claude Code