-
Notifications
You must be signed in to change notification settings - Fork 431
Support nullable #535
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
Support nullable #535
Changes from all commits
9f42f43
f4fbe87
ad4ee38
6982494
84cff45
061ad03
42f3676
caf143c
988a6f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import {JSONSchemaTypeName, LinkedJSONSchema, NormalizedJSONSchema, Parent} from './types/JSONSchema' | ||
| import {appendToDescription, escapeBlockComment, isSchemaLike, justName, toSafeString, traverse} from './utils' | ||
| import {appendToDescription, escapeBlockComment, isSchemaLike, justName, toSafeString, traverse, warning} from './utils' | ||
| import {Options} from './' | ||
| import {DereferencedPaths} from './resolver' | ||
| import {isDeepStrictEqual} from 'util' | ||
|
|
@@ -215,6 +215,35 @@ rules.set('Transform definitions to $defs', (schema, fileName) => { | |
| } | ||
| }) | ||
|
|
||
| rules.set('Transform nullable to null type', schema => { | ||
| if (schema.nullable !== true) { | ||
| return | ||
| } | ||
|
|
||
| delete schema.nullable | ||
|
|
||
| if (schema.const !== undefined) { | ||
| if (schema.const !== null) { | ||
| warning('normalizer', 'const should be set to null when schema is nullable', schema) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I understand why you added The only location (see my comment on the recommended change in |
||
| schema.enum = [schema.const, null] | ||
| delete schema.const | ||
| } | ||
| } else if (schema.enum) { | ||
| if (!schema.enum.includes(null)) { | ||
| warning('normalizer', 'enum should include null when schema is nullable', schema) | ||
| schema.enum.push(null) | ||
| } | ||
| } else if (schema.type) { | ||
| if (Array.isArray(schema.type)) { | ||
| if (!schema.type.includes('null')) { | ||
| schema.type.push('null') | ||
| } | ||
| } else if (schema.type !== 'null') { | ||
| schema.type = [schema.type, 'null'] | ||
| } | ||
| } | ||
| }) | ||
|
|
||
| rules.set('Transform const to singleton enum', schema => { | ||
| if (schema.const !== undefined) { | ||
| schema.enum = [schema.const] | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -209,6 +209,13 @@ export function error(...messages: any[]): void { | |||||||||||||||||||||||||||||
| console.error(getStyledTextForLogging('red')?.('error'), ...messages) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export function warning(...messages: any[]): void { | ||||||||||||||||||||||||||||||
| if (!process.env.VERBOSE) { | ||||||||||||||||||||||||||||||
| return console.warn(messages) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| console.warn(getStyledTextForLogging('yellow')?.('warning'), ...messages) | ||||||||||||||||||||||||||||||
|
Comment on lines
+213
to
+216
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will always log. If you look at the Since this is just a warning I recommend the following change:
Suggested change
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was written to mimic json-schema-to-typescript/src/utils.ts Lines 205 to 210 in 6adcad9
I don't pretend to understand that choice and it's not my aim to question it. I'm just following precedence here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I figured, but I believe that is only done in the |
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| type LogStyle = 'blue' | 'cyan' | 'green' | 'magenta' | 'red' | 'white' | 'yellow' | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| export function log(style: LogStyle, title: string, ...messages: unknown[]): void { | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| export const input = { | ||
| type: 'object', | ||
| properties: { | ||
| a: { | ||
| const: '', | ||
| nullable: true, | ||
| }, | ||
| b: { | ||
| const: null, | ||
| nullable: true, | ||
| }, | ||
| c: { | ||
| enum: ['a', 'b'], | ||
| nullable: true, | ||
| }, | ||
| d: { | ||
| enum: ['', null], | ||
| nullable: true, | ||
| }, | ||
| e: { | ||
| type: 'string', | ||
| nullable: true, | ||
| }, | ||
| f: { | ||
| type: 'null', | ||
| nullable: true, | ||
| }, | ||
| g: { | ||
| type: 'string', | ||
| const: '', | ||
| nullable: true, | ||
| }, | ||
| h: { | ||
| type: 'string', | ||
| const: null, | ||
| nullable: true, | ||
| }, | ||
| i: { | ||
| type: 'string', | ||
| enum: ['a', 'b'], | ||
| nullable: true, | ||
| }, | ||
| j: { | ||
| type: 'string', | ||
| enum: ['', null], | ||
| nullable: true, | ||
| }, | ||
| k: { | ||
| type: ['string', 'integer'], | ||
| nullable: true, | ||
| }, | ||
| l: { | ||
| type: ['string', 'null'], | ||
| nullable: true, | ||
| }, | ||
| }, | ||
| additionalProperties: false, | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| { | ||
| "name": "Nullable adds null to type", | ||
| "in": { | ||
| "$id": "a", | ||
| "type": "object", | ||
| "properties": { | ||
| "a": { | ||
| "const": "", | ||
| "nullable": true | ||
| }, | ||
| "b": { | ||
| "const": null, | ||
| "nullable": true | ||
| }, | ||
| "c": { | ||
| "enum": ["a", "b"], | ||
| "nullable": true | ||
| }, | ||
| "d": { | ||
| "enum": ["", null], | ||
| "nullable": true | ||
| }, | ||
| "e": { | ||
| "type": "string", | ||
| "nullable": true | ||
| }, | ||
| "f": { | ||
| "type": "null", | ||
| "nullable": true | ||
| }, | ||
| "g": { | ||
| "type": "string", | ||
| "const": "", | ||
| "nullable": true | ||
| }, | ||
| "h": { | ||
| "type": "string", | ||
| "const": null, | ||
| "nullable": true | ||
| }, | ||
| "i": { | ||
| "type": "string", | ||
| "enum": ["a", "b"], | ||
| "nullable": true | ||
| }, | ||
| "j": { | ||
| "type": "string", | ||
| "enum": ["", null], | ||
| "nullable": true | ||
| }, | ||
| "k": { | ||
| "type": ["string", "integer"], | ||
| "nullable": true | ||
| }, | ||
| "l": { | ||
| "type": ["string", "null"], | ||
| "nullable": true | ||
| } | ||
| }, | ||
| "required": [], | ||
| "additionalProperties": false | ||
| }, | ||
| "out": { | ||
| "$id": "a", | ||
| "type": "object", | ||
| "properties": { | ||
| "a": { | ||
| "enum": ["", null] | ||
| }, | ||
| "b": { | ||
| "enum": [null] | ||
| }, | ||
| "c": { | ||
| "enum": ["a", "b", null] | ||
| }, | ||
| "d": { | ||
| "enum": ["", null] | ||
| }, | ||
| "e": { | ||
| "type": ["string", "null"] | ||
| }, | ||
| "f": { | ||
| "type": "null" | ||
| }, | ||
| "g": { | ||
| "type": "string", | ||
| "enum": ["", null] | ||
| }, | ||
| "h": { | ||
| "type": "string", | ||
| "enum": [null] | ||
| }, | ||
| "i": { | ||
| "type": "string", | ||
| "enum": ["a", "b", null] | ||
| }, | ||
| "j": { | ||
| "type": "string", | ||
| "enum": ["", null] | ||
| }, | ||
| "k": { | ||
| "type": ["string", "integer", "null"] | ||
| }, | ||
| "l": { | ||
| "type": ["string", "null"] | ||
| } | ||
| }, | ||
| "required": [], | ||
| "additionalProperties": false | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.