-
-
Notifications
You must be signed in to change notification settings - Fork 208
Add support for array of types in schema definition #78
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| 'use strict' | ||
|
|
||
| const test = require('tap').test | ||
| const build = require('..') | ||
|
|
||
| test('simple object with multi-type property', (t) => { | ||
| t.plan(2) | ||
|
|
||
| const schema = { | ||
| title: 'simple object with multi-type property', | ||
| type: 'object', | ||
| properties: { | ||
| stringOrNumber: { | ||
| type: ['string', 'number'] | ||
| } | ||
| } | ||
| } | ||
| const stringify = build(schema) | ||
|
|
||
| try { | ||
| const value = stringify({ | ||
| stringOrNumber: 'string' | ||
| }) | ||
| t.is(value, '{"stringOrNumber":"string"}') | ||
| } catch (e) { | ||
| t.fail() | ||
| } | ||
|
|
||
| try { | ||
| const value = stringify({ | ||
| stringOrNumber: 42 | ||
| }) | ||
| t.is(value, '{"stringOrNumber":42}') | ||
| } catch (e) { | ||
| t.fail() | ||
| } | ||
| }) | ||
|
|
||
| test('object with array of multiple types', (t) => { | ||
| t.plan(3) | ||
|
|
||
| const schema = { | ||
| title: 'object with array of multiple types', | ||
| type: 'object', | ||
| properties: { | ||
| arrayOfStringsAndNumbers: { | ||
| type: 'array', | ||
| items: { | ||
| type: ['string', 'number'] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| const stringify = build(schema) | ||
|
|
||
| try { | ||
| const value = stringify({ | ||
| arrayOfStringsAndNumbers: ['string1', 'string2'] | ||
| }) | ||
| t.is(value, '{"arrayOfStringsAndNumbers":["string1","string2"]}') | ||
| } catch (e) { | ||
| console.log(e) | ||
| t.fail() | ||
| } | ||
|
|
||
| try { | ||
| const value = stringify({ | ||
| arrayOfStringsAndNumbers: [42, 7] | ||
| }) | ||
| t.is(value, '{"arrayOfStringsAndNumbers":[42,7]}') | ||
| } catch (e) { | ||
| t.fail() | ||
| } | ||
|
|
||
| try { | ||
| const value = stringify({ | ||
| arrayOfStringsAndNumbers: ['string1', 42, 7, 'string2'] | ||
| }) | ||
| t.is(value, '{"arrayOfStringsAndNumbers":["string1",42,7,"string2"]}') | ||
| } catch (e) { | ||
| t.fail() | ||
| } | ||
| }) | ||
|
|
||
| test('object with tuple of multiple types', (t) => { | ||
| t.plan(2) | ||
|
|
||
| const schema = { | ||
| title: 'object with array of multiple types', | ||
| type: 'object', | ||
| properties: { | ||
| fixedTupleOfStringsAndNumbers: { | ||
| type: 'array', | ||
| items: [ | ||
| { | ||
| type: 'string' | ||
| }, | ||
| { | ||
| type: 'number' | ||
| }, | ||
| { | ||
| type: ['string', 'number'] | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| const stringify = build(schema) | ||
|
|
||
| try { | ||
| const value = stringify({ | ||
| fixedTupleOfStringsAndNumbers: ['string1', 42, 7] | ||
| }) | ||
| t.is(value, '{"fixedTupleOfStringsAndNumbers":["string1",42,7]}') | ||
| } catch (e) { | ||
| console.log(e) | ||
| t.fail() | ||
| } | ||
|
|
||
| try { | ||
| const value = stringify({ | ||
| fixedTupleOfStringsAndNumbers: ['string1', 42, 'string2'] | ||
| }) | ||
| t.is(value, '{"fixedTupleOfStringsAndNumbers":["string1",42,"string2"]}') | ||
| } catch (e) { | ||
| console.log(e) | ||
| t.fail() | ||
| } | ||
| }) | ||
|
|
||
| test('object with anyOf and multiple types', (t) => { | ||
| t.plan(3) | ||
|
|
||
| const schema = { | ||
| title: 'object with anyOf and multiple types', | ||
| type: 'object', | ||
| properties: { | ||
| objectOrBoolean: { | ||
| anyOf: [ | ||
| { | ||
| type: 'object', | ||
| properties: { | ||
| stringOrNumber: { | ||
| type: ['string', 'number'] | ||
| } | ||
| } | ||
| }, | ||
| { | ||
| type: 'boolean' | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| const stringify = build(schema) | ||
|
|
||
| try { | ||
| const value = stringify({ | ||
| objectOrBoolean: { stringOrNumber: 'string' } | ||
| }) | ||
| t.is(value, '{"objectOrBoolean":{"stringOrNumber":"string"}}') | ||
| } catch (e) { | ||
| console.log(e) | ||
| t.fail() | ||
| } | ||
|
|
||
| try { | ||
| const value = stringify({ | ||
| objectOrBoolean: { stringOrNumber: 42 } | ||
| }) | ||
| t.is(value, '{"objectOrBoolean":{"stringOrNumber":42}}') | ||
| } catch (e) { | ||
| t.fail() | ||
| } | ||
|
|
||
| try { | ||
| const value = stringify({ | ||
| objectOrBoolean: true | ||
| }) | ||
| t.is(value, '{"objectOrBoolean":true}') | ||
| } catch (e) { | ||
| t.fail() | ||
| } | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
There is potential for an optimization here. In the current implementation the generated code checks if the item matches any of the types. The
nested()function basically performs the same check again using ajv. We could split this condition here into separate ones and use the knowledge of the item's type for thenested()function. However, this requires some more refactoring.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.
Feel free to experiment! We do like optimizations! :D
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.
I'm landing this now, feel free to send a followup PR with the optimizations!