Support undefined, number, and object in JSON module#3980
Conversation
The previous implementation of `optional` is now `optionalOrNull`.
There was a problem hiding this comment.
Warning
- Copilot's review of this pull request may be incomplete because some of the changed files are excluded by your Copilot content exclusion settings. See Excluding content from Copilot for details.
Pull request overview
This PR extends the internal JSON schema-validation helpers to better distinguish “optional” vs “optional-or-null” fields, and to add first-class validators for number and object. It also updates start-proxy configuration schemas to use the new optionalOrNull semantics and adjusts unit tests accordingly.
Changes:
- Introduces
isNumber, plusjson.numberandjson.objectvalidators. - Renames the old “optional (undefined or null)” behavior to
optionalOrNulland redefinesoptionalto mean “undefined-only”. - Updates start-proxy schemas and unit tests to reflect the new optional semantics.
Show a summary per file
| File | Description |
|---|---|
| src/start-proxy/types.ts | Switches relevant schema fields from optional to optionalOrNull for backwards-compatible null acceptance. |
| src/json/index.ts | Adds number/object validators and splits optional handling into optionalOrNull vs optional. |
| src/json/index.test.ts | Updates/expands tests to cover the new optional semantics (optionalOrNull vs optional). |
| lib/entry-points.js | Generated output (content excluded from review per policy). |
Review details
Files excluded by content exclusion policy (1)
- lib/entry-points.js
- Files reviewed: 3/4 changed files
- Comments generated: 3
- Review effort level: Low
| /** A validator for number fields in schemas. */ | ||
| export const number = { | ||
| validate: isNumber, | ||
| required: true, | ||
| } as const satisfies Validator<number>; |
Using `unknown` meant that the validated object would not be indexable because its type would devolve to `{}`
These have been spun off into #3980.
| export const object = { | ||
| validate: isObject<any>, | ||
| required: true, | ||
| } as const satisfies Validator<UnvalidatedObject<any>>; |
There was a problem hiding this comment.
For top-level objects, we'd call validateSchema on them along with the relevant schema. Adding this object validator to schemas seems a bit simplistic. Is the intention here to say "I want to accept an arbitrary object"? If so, then it might be better off as being named any or similar, rather than object. If not, then perhaps this should be parameterised over a schema:
| export const object = { | |
| validate: isObject<any>, | |
| required: true, | |
| } as const satisfies Validator<UnvalidatedObject<any>>; | |
| export const object = <S extends Schema>(schema: S) => ...; |
There was a problem hiding this comment.
Here's the use-case:
json.validateSchema(
{
version: json.string,
features: json.optional(json.object),
overlayVersion: json.optional(json.number),
},
x,
);where features here refers to VersionInfo.features.
There was a problem hiding this comment.
OK, so there is a schema for features (a mapping of strings to booleans) that could be expressed if we wanted to. I think it's better for us to leave the conversation about the schema validation for the CLI output to the other PR, but the relevant flow chart for my comment was:
- If you just want to validate arbitrary objects, rename it to
anyor similar instead ofobject. - If not, change
objectto validate a specific object schema.
46563f5 to
2a939e7
Compare
Summary
This pull request introduces several additions to the JSON schema-validation utilities:
optionalvalidator tooptionalOrNullto clarify that schema fields may be optional and acceptnullas a valid value, in addition toundefined.optionalvalidator so that it only acceptsundefined(notnull), making its behavior more precise and distinct fromoptionalOrNull.numberandobjecttypes.isNumbertype guard function for type checking.This PR is a prerequisite for #3950.
Risk assessment
For internal use only. Please select the risk level of this change:
Which use cases does this change impact?
Workflow types:
dynamicworkflows (Default Setup, Code Quality, ...).Products:
analysis-kinds: code-scanning.analysis-kinds: code-quality.Environments:
github.comand/or GitHub Enterprise Cloud with Data Residency.How did/will you validate this change?
.test.tsfiles).pr-checks).If something goes wrong after this change is released, what are the mitigation and rollback strategies?
How will you know if something goes wrong after this change is released?
Are there any special considerations for merging or releasing this change?
Merge / deployment checklist