-
Notifications
You must be signed in to change notification settings - Fork 378
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
Add x-enum-varnames
support
#405
Changes from all commits
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 |
---|---|---|
|
@@ -29,6 +29,10 @@ export interface JSONSchema extends JSONSchema4 { | |
* schema extension to support numeric enums | ||
*/ | ||
tsEnumNames?: string[] | ||
/** | ||
* schema extension to support numeric enums | ||
*/ | ||
'x-enum-varnames'?: string[] | ||
/** | ||
* schema extension to support custom types | ||
*/ | ||
|
@@ -93,10 +97,18 @@ export interface EnumJSONSchema extends NormalizedJSONSchema { | |
enum: any[] | ||
} | ||
|
||
export interface NamedEnumJSONSchema extends NormalizedJSONSchema { | ||
interface DeprecatedNamedEnumJSONSchema extends NormalizedJSONSchema { | ||
'x-enum-varnames'?: never | ||
tsEnumNames: string[] | ||
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. Normalized schemas shouldn't contain 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. So I should delete 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. Yeah just add |
||
} | ||
|
||
interface _NamedEnumJSONSchema extends NormalizedJSONSchema { | ||
'x-enum-varnames': string[] | ||
tsEnumNames?: never | ||
} | ||
|
||
export type NamedEnumJSONSchema = _NamedEnumJSONSchema | DeprecatedNamedEnumJSONSchema | ||
|
||
export interface SchemaSchema extends NormalizedJSONSchema { | ||
properties: { | ||
[k: string]: NormalizedJSONSchema | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,32 @@ import {mapDeep} from './utils' | |
type Rule = (schema: JSONSchema) => boolean | void | ||
const rules = new Map<string, Rule>() | ||
|
||
rules.set('Enum members and x-enum-varnames must be of the same length', schema => { | ||
if (schema.enum && schema['x-enum-varnames'] && schema.enum.length !== schema['x-enum-varnames'].length) { | ||
return false | ||
} | ||
}) | ||
|
||
/** | ||
* `tsEnumNames` is being replaced by `x-enum-varnames`, but we'll leave this | ||
* validator until `tsEnumNames` is removed. | ||
*/ | ||
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. Please make sure this is still covered by tests. |
||
rules.set('Enum members and tsEnumNames must be of the same length', schema => { | ||
if (schema.enum && schema.tsEnumNames && schema.enum.length !== schema.tsEnumNames.length) { | ||
return false | ||
} | ||
}) | ||
|
||
rules.set('x-enum-varnames must be an array of strings', schema => { | ||
if (schema['x-enum-varnames'] && schema['x-enum-varnames'].some(_ => typeof _ !== 'string')) { | ||
return false | ||
} | ||
}) | ||
|
||
/** | ||
* `tsEnumNames` is being replaced by `x-enum-varnames`, but we'll leave this | ||
* validator until `tsEnumNames` is removed. | ||
*/ | ||
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. Please make sure this is still covered by tests. 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. Should I copy 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. Sounds ok to me, or merge into a single test file that covers all the cases (x-, ts-, x- and ts-, etc.). |
||
rules.set('tsEnumNames must be an array of strings', schema => { | ||
if (schema.tsEnumNames && schema.tsEnumNames.some(_ => typeof _ !== 'string')) { | ||
return false | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mind being a little more lenient, and only throwing if the two don't match? These should also have tests, if possible.
ie.