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(utils): add to JSONSchema4Type missing Array and Object #7406

Merged
merged 7 commits into from
Aug 10, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function generateUnionType(
if (Array.isArray(memberSchema)) {
throw new NotSupportedError('array in an enum', memberSchema);
}
return generateType(memberSchema, refMap);
return generateType(memberSchema as JSONSchema4, refMap);
}
})(),
);
Expand Down
24 changes: 20 additions & 4 deletions packages/utils/src/json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,23 @@
/**
* @see https://tools.ietf.org/html/draft-zyp-json-schema-04#section-3.5
*/
export type JSONSchema4Type = boolean | number | string | null;
export type JSONSchema4Type =
| boolean
| JSONSchema4Array
| JSONSchema4Object
Copy link
Member

Choose a reason for hiding this comment

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

I don't think it is the best thing for us to add this here because JSONSchema4Type is used in the enum property where arrays and objects are disallowed (slash non-sensical).

So I think it makes sense for us to define a new type JSONSchema4DefaultValue which includes all of the valid default values.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bradzacher Updated with a separate type named JSONSchema4TypeExtended, which I thought could be a better name, but can change it to JSONSchema4DefaultValue if needed

| number
| string
| null;

// Workaround for infinite type recursion
export interface JSONSchema4Object {

Check failure on line 37 in packages/utils/src/json-schema.ts

View workflow job for this annotation

GitHub Actions / Lint (lint)

A record is preferred over an index signature
[key: string]: JSONSchema4Type;
}

// Workaround for infinite type recursion
// https://github.com/Microsoft/TypeScript/issues/3496#issuecomment-128553540
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface JSONSchema4Array extends Array<JSONSchema4Type> {}

/**
* Meta schema
Expand All @@ -51,7 +67,7 @@
| JSONSchema4AnyOfSchema
| JSONSchema4AnySchema
| JSONSchema4ArraySchema
| JSONSchema4BoleanSchema
| JSONSchema4BooleanSchema
| JSONSchema4MultiSchema
| JSONSchema4NullSchema
| JSONSchema4NumberSchema
Expand Down Expand Up @@ -195,7 +211,7 @@
Omit<JSONSchema4ArraySchema, 'enum' | 'type'>,
Omit<JSONSchema4StringSchema, 'enum' | 'type'>,
Omit<JSONSchema4NumberSchema, 'enum' | 'type'>,
Omit<JSONSchema4BoleanSchema, 'enum' | 'type'>,
Omit<JSONSchema4BooleanSchema, 'enum' | 'type'>,
Omit<JSONSchema4NullSchema, 'enum' | 'type'>,
Omit<JSONSchema4AnySchema, 'enum' | 'type'> {
type: JSONSchema4TypeName[];
Expand Down Expand Up @@ -460,7 +476,7 @@
/**
* @see https://json-schema.org/understanding-json-schema/reference/boolean.html
*/
export interface JSONSchema4BoleanSchema extends JSONSchema4Base {
export interface JSONSchema4BooleanSchema extends JSONSchema4Base {
Copy link
Member

Choose a reason for hiding this comment

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

goodcatch

Spelling is hard 😅
Technically this is a breaking change but given it's a cock up I'm fine with doing this here. Given nobody noticed I doubt anyone is using it directly yet!

type: 'boolean';

/**
Expand Down