-
-
Notifications
You must be signed in to change notification settings - Fork 886
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
Extend discriminator to support number and boolean values #1729
Open
pkuczynski
wants to merge
9
commits into
ajv-validator:master
Choose a base branch
from
pkuczynski:discriminator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ce7a34b
Extend discriminator to support number and boolean values
pkuczynski c7938f0
Merge branch 'master' into discriminator
epoberezkin b442098
Merge branch 'master' into discriminator
pkuczynski 85d65f9
apply code feedback
pkuczynski 813f8cf
Merge branch 'master' into discriminator
epoberezkin 364dfde
Merge branch 'master' into discriminator
pkuczynski 65ff2d2
Merge branch 'master' into discriminator
pkuczynski a3600bf
Merge branch 'master' into discriminator
jasoniangreen 0b42fcf
Merge branch 'master' into discriminator
pkuczynski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ const def: CodeKeywordDefinition = { | |
const valid = gen.let("valid", false) | ||
const tag = gen.const("tag", _`${data}${getProperty(tagName)}`) | ||
gen.if( | ||
_`typeof ${tag} == "string"`, | ||
_`["string", "number", "boolean"].includes(typeof ${tag})`, | ||
() => validateMapping(), | ||
() => cxt.error(false, {discrError: DiscrError.Tag, tag, tagName}) | ||
) | ||
|
@@ -41,9 +41,9 @@ const def: CodeKeywordDefinition = { | |
function validateMapping(): void { | ||
const mapping = getMapping() | ||
gen.if(false) | ||
for (const tagValue in mapping) { | ||
for (const tagValue of mapping.keys()) { | ||
gen.elseIf(_`${tag} === ${tagValue}`) | ||
gen.assign(valid, applyTagSchema(mapping[tagValue])) | ||
gen.assign(valid, applyTagSchema(mapping.get(tagValue))) | ||
} | ||
gen.else() | ||
cxt.error(false, {discrError: DiscrError.Mapping, tag, tagName}) | ||
|
@@ -57,8 +57,10 @@ const def: CodeKeywordDefinition = { | |
return _valid | ||
} | ||
|
||
function getMapping(): {[T in string]?: number} { | ||
const oneOfMapping: {[T in string]?: number} = {} | ||
type OneOfMapping = Map<string | number | boolean, number> | ||
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 would also add null. |
||
|
||
function getMapping(): OneOfMapping { | ||
const oneOfMapping: OneOfMapping = new Map() | ||
const topRequired = hasRequired(parentSchema) | ||
let tagRequired = true | ||
for (let i = 0; i < oneOf.length; i++) { | ||
|
@@ -78,7 +80,7 @@ const def: CodeKeywordDefinition = { | |
} | ||
|
||
function addMappings(sch: AnySchemaObject, i: number): void { | ||
if (sch.const) { | ||
if (sch.const !== undefined) { | ||
addMapping(sch.const, i) | ||
} else if (sch.enum) { | ||
for (const tagValue of sch.enum) { | ||
|
@@ -89,11 +91,16 @@ const def: CodeKeywordDefinition = { | |
} | ||
} | ||
|
||
function addMapping(tagValue: unknown, i: number): void { | ||
if (typeof tagValue != "string" || tagValue in oneOfMapping) { | ||
throw new Error(`discriminator: "${tagName}" values must be unique strings`) | ||
function addMapping(tagValue: any, i: number): void { | ||
if ( | ||
!["string", "number", "boolean"].includes(typeof tagValue) || | ||
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. +null |
||
oneOfMapping.has(tagValue) | ||
) { | ||
throw new Error( | ||
`discriminator: "${tagName}" values must be unique strings, numbers or booleans` | ||
) | ||
} | ||
oneOfMapping[tagValue] = i | ||
oneOfMapping.set(tagValue, i) | ||
} | ||
} | ||
}, | ||
|
This file contains 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
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.
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.
This can be improved. The compilation phase should collect all tags and in case they are all the same type, this type would be used. In general case it should use something like
or(...tagTypes.map((t) => _`typeof ${tag} == ${t}`))
-include
call is inefficient. This general case is likely to be ok fortagTypes
with a single item, if not -or
function can be improved (but I think it should just work).