Skip to content
Snippets Groups Projects
Commit 61d1e485 authored by Lars Trieloff's avatar Lars Trieloff
Browse files

Support for nullable types and type choices #87

parent 81b275c2
No related branches found
No related tags found
No related merge requests found
Showing
with 306 additions and 101 deletions
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
* [Simple](./simple.schema.md)`https://example.com/schemas/simple` (Unknown) * [Simple](./simple.schema.md)`https://example.com/schemas/simple` (Unknown)
* [Simple Types](./simpletypes.schema.md)`https://example.com/schemas/simpletypes` (Unknown) * [Simple Types](./simpletypes.schema.md)`https://example.com/schemas/simpletypes` (Unknown)
* [Stabilizing](./stabilizing.schema.md)`https://example.com/schemas/stabilizing` (Stabilizing) * [Stabilizing](./stabilizing.schema.md)`https://example.com/schemas/stabilizing` (Stabilizing)
* [Type Arrays](./typearrays.schema.md)`https://example.com/schemas/typearrays` (Unknown)
## /subdir/ ## /subdir/
......
...@@ -17,17 +17,17 @@ This is an example schema with examples for multiple array types and their const ...@@ -17,17 +17,17 @@ This is an example schema with examples for multiple array types and their const
# Arrays Properties # Arrays Properties
| Property | Type | Required | Defined by | | Property | Type | Required | Nullable | Defined by |
|----------|------|----------|------------| |----------|------|----------|----------|------------|
| [boollist](#boollist) | `boolean[]` | Optional | Arrays (this schema) | | [boollist](#boollist) | `boolean[]` | Optional | No | Arrays (this schema) |
| [coordinatelist](#coordinatelist) | `number[][]` | Optional | Arrays (this schema) | | [coordinatelist](#coordinatelist) | `number[][]` | Optional | No | Arrays (this schema) |
| [intlist](#intlist) | `integer[]` | Optional | Arrays (this schema) | | [intlist](#intlist) | `integer[]` | Optional | No | Arrays (this schema) |
| [list](#list) | `string[]` | Optional | Arrays (this schema) | | [list](#list) | `string[]` | Optional | No | Arrays (this schema) |
| [listlist](#listlist) | `array[]` | Optional | Arrays (this schema) | | [listlist](#listlist) | `array[]` | Optional | No | Arrays (this schema) |
| [numlist](#numlist) | `number[]` | Optional | Arrays (this schema) | | [numlist](#numlist) | `number[]` | Optional | No | Arrays (this schema) |
| [objectlist](#objectlist) | `object[]` | Optional | Arrays (this schema) | | [objectlist](#objectlist) | `object[]` | Optional | No | Arrays (this schema) |
| [stringlistlist](#stringlistlist) | `string[][]` | Optional | Arrays (this schema) | | [stringlistlist](#stringlistlist) | `string[][]` | Optional | No | Arrays (this schema) |
| `*` | any | Additional | this schema *allows* additional properties | | `*` | any | Additional | Yes | this schema *allows* additional properties |
## boollist ## boollist
......
...@@ -23,17 +23,17 @@ This is an example schema that uses types defined in other schemas. ...@@ -23,17 +23,17 @@ This is an example schema that uses types defined in other schemas.
# Complex References Properties # Complex References Properties
| Property | Type | Required | Defined by | | Property | Type | Required | Nullable | Defined by |
|----------|------|----------|------------| |----------|------|----------|----------|------------|
| [and](#and) | complex | Optional | Complex References (this schema) | | [and](#and) | complex | Optional | No | Complex References (this schema) |
| [or](#or) | complex | Optional | Complex References (this schema) | | [or](#or) | complex | Optional | No | Complex References (this schema) |
| [refabstract](#refabstract) | `object` | **Required** | Complex References (this schema) | | [refabstract](#refabstract) | `object` | **Required** | No | Complex References (this schema) |
| [reflist](#reflist) | Simple | Optional | Complex References (this schema) | | [reflist](#reflist) | Simple | Optional | No | Complex References (this schema) |
| [refnamed](#refnamed) | Simple | Optional | Complex References (this schema) | | [refnamed](#refnamed) | Simple | Optional | No | Complex References (this schema) |
| [xor](#xor) | complex | Optional | Complex References (this schema) | | [xor](#xor) | complex | Optional | No | Complex References (this schema) |
| `int.*` | `integer` | Pattern | Complex References (this schema) | | `int.*` | `integer` | Pattern No | Complex References (this schema) |
| `str.*` | `string` | Pattern | Complex References (this schema) | | `str.*` | `string` | Pattern No | Complex References (this schema) |
| `*` | any | Additional | this schema *allows* additional properties | | `*` | any | Additional | Yes | this schema *allows* additional properties |
## and ## and
......
...@@ -17,10 +17,10 @@ This is an example schema with examples for properties with constant values ...@@ -17,10 +17,10 @@ This is an example schema with examples for properties with constant values
# Constant Types Properties # Constant Types Properties
| Property | Type | Required | Defined by | | Property | Type | Required | Nullable | Defined by |
|----------|------|----------|------------| |----------|------|----------|----------|------------|
| [hello](#hello) | `const` | **Required** | Constant Types (this schema) | | [hello](#hello) | `const` | **Required** | No | Constant Types (this schema) |
| `*` | any | Additional | this schema *allows* additional properties | | `*` | any | Additional | Yes | this schema *allows* additional properties |
## hello ## hello
......
...@@ -17,11 +17,11 @@ This is an extensible schema. It has `definitions`, that can be used in other sc ...@@ -17,11 +17,11 @@ This is an extensible schema. It has `definitions`, that can be used in other sc
# Custom Properties # Custom Properties
| Property | Type | Required | Defined by | | Property | Type | Required | Nullable | Defined by |
|----------|------|----------|------------| |----------|------|----------|----------|------------|
| [bar](#bar) | `string` | Optional | Custom (this schema) | | [bar](#bar) | `string` | Optional | No | Custom (this schema) |
| [foo](#foo) | `string` | Optional | Custom (this schema) | | [foo](#foo) | `string` | Optional | No | Custom (this schema) |
| `*` | any | Additional | this schema *allows* additional properties | | `*` | any | Additional | Yes | this schema *allows* additional properties |
## bar ## bar
......
...@@ -24,15 +24,15 @@ This is an extending schema. It is extending another extending schema. It pulls ...@@ -24,15 +24,15 @@ This is an extending schema. It is extending another extending schema. It pulls
# Deeply Extending Properties # Deeply Extending Properties
| Property | Type | Required | Defined by | | Property | Type | Required | Nullable | Defined by |
|----------|------|----------|------------| |----------|------|----------|----------|------------|
| [@id](#id) | `string` | Optional | [Definitions](definitions.schema.md#id) | | [@id](#id) | `string` | Optional | No | [Definitions](definitions.schema.md#id) |
| [bar](#bar) | `string` | Optional | [Extensible](extensible.schema.md#bar) | | [bar](#bar) | `string` | Optional | No | [Extensible](extensible.schema.md#bar) |
| [baz](#baz) | `string` | Optional | [Extending](extending.schema.md#baz) | | [baz](#baz) | `string` | Optional | No | [Extending](extending.schema.md#baz) |
| [hey](#hey) | `string` | Optional | Deeply Extending (this schema) | | [hey](#hey) | `string` | Optional | No | Deeply Extending (this schema) |
| [id](#id-1) | `string` | Optional | [Definitions](definitions.schema.md#id-1) | | [id](#id-1) | `string` | Optional | No | [Definitions](definitions.schema.md#id-1) |
| [meta:id](#metaid) | `string` | Optional | [Definitions](definitions.schema.md#metaid) | | [meta:id](#metaid) | `string` | Optional | No | [Definitions](definitions.schema.md#metaid) |
| `*` | any | Additional | this schema *allows* additional properties | | `*` | any | Additional | Yes | this schema *allows* additional properties |
## @id ## @id
......
...@@ -34,12 +34,12 @@ aks. ...@@ -34,12 +34,12 @@ aks.
# Definitions Properties # Definitions Properties
| Property | Type | Required | Defined by | | Property | Type | Required | Nullable | Defined by |
|----------|------|----------|------------| |----------|------|----------|----------|------------|
| [@id](#id) | `string` | Optional | Definitions (this schema) | | [@id](#id) | `string` | Optional | No | Definitions (this schema) |
| [id](#id-1) | `string` | **Required** | Definitions (this schema) | | [id](#id-1) | `string` | **Required** | No | Definitions (this schema) |
| [meta:id](#metaid) | `string` | Optional | Definitions (this schema) | | [meta:id](#metaid) | `string` | Optional | No | Definitions (this schema) |
| `*` | any | Additional | this schema *allows* additional properties | | `*` | any | Additional | Yes | this schema *allows* additional properties |
## @id ## @id
......
...@@ -17,11 +17,11 @@ This is an example schema with examples for properties with enum values ...@@ -17,11 +17,11 @@ This is an example schema with examples for properties with enum values
# Enumerated Properties # Enumerated Properties
| Property | Type | Required | Defined by | | Property | Type | Required | Nullable | Defined by |
|----------|------|----------|------------| |----------|------|----------|----------|------------|
| [hello](#hello) | `enum` | **Required** | Enumerated (this schema) | | [hello](#hello) | `enum` | **Required** | No | Enumerated (this schema) |
| [nested](#nested) | `object` | Optional | Enumerated (this schema) | | [nested](#nested) | `object` | Optional | No | Enumerated (this schema) |
| `*` | any | Additional | this schema *allows* additional properties | | `*` | any | Additional | Yes | this schema *allows* additional properties |
## hello ## hello
......
...@@ -25,11 +25,11 @@ This is an example schema with examples. Too many examples? There can never be t ...@@ -25,11 +25,11 @@ This is an example schema with examples. Too many examples? There can never be t
# Example Properties # Example Properties
| Property | Type | Required | Defined by | | Property | Type | Required | Nullable | Defined by |
|----------|------|----------|------------| |----------|------|----------|----------|------------|
| [bar](#bar) | `string` | Optional | Example (this schema) | | [bar](#bar) | `string` | Optional | No | Example (this schema) |
| [foo](#foo) | `string` | Optional | Example (this schema) | | [foo](#foo) | `string` | Optional | No | Example (this schema) |
| `*` | any | Additional | this schema *allows* additional properties | | `*` | any | Additional | Yes | this schema *allows* additional properties |
## bar ## bar
......
...@@ -34,11 +34,11 @@ This is an example schema with *multiple* examples. Too many examples? There can ...@@ -34,11 +34,11 @@ This is an example schema with *multiple* examples. Too many examples? There can
# Examples Properties # Examples Properties
| Property | Type | Required | Defined by | | Property | Type | Required | Nullable | Defined by |
|----------|------|----------|------------| |----------|------|----------|----------|------------|
| [bar](#bar) | `string` | **Required** | Examples (this schema) | | [bar](#bar) | `string` | **Required** | No | Examples (this schema) |
| [foo](#foo) | `string` | Optional | Examples (this schema) | | [foo](#foo) | `string` | Optional | No | Examples (this schema) |
| `*` | any | Additional | this schema *allows* additional properties | | `*` | any | Additional | Yes | this schema *allows* additional properties |
## bar ## bar
......
...@@ -23,14 +23,14 @@ This is an extending schema. It pulls `definitions` from other schemas. ...@@ -23,14 +23,14 @@ This is an extending schema. It pulls `definitions` from other schemas.
# Extending Properties # Extending Properties
| Property | Type | Required | Defined by | | Property | Type | Required | Nullable | Defined by |
|----------|------|----------|------------| |----------|------|----------|----------|------------|
| [@id](#id) | `string` | Optional | [Definitions](definitions.schema.md#id) | | [@id](#id) | `string` | Optional | No | [Definitions](definitions.schema.md#id) |
| [bar](#bar) | `string` | Optional | [Extensible](extensible.schema.md#bar) | | [bar](#bar) | `string` | Optional | No | [Extensible](extensible.schema.md#bar) |
| [baz](#baz) | `string` | Optional | Extending (this schema) | | [baz](#baz) | `string` | Optional | No | Extending (this schema) |
| [id](#id-1) | `string` | Optional | [Definitions](definitions.schema.md#id-1) | | [id](#id-1) | `string` | Optional | No | [Definitions](definitions.schema.md#id-1) |
| [meta:id](#metaid) | `string` | Optional | [Definitions](definitions.schema.md#metaid) | | [meta:id](#metaid) | `string` | Optional | No | [Definitions](definitions.schema.md#metaid) |
| `*` | any | Additional | this schema *allows* additional properties | | `*` | any | Additional | Yes | this schema *allows* additional properties |
## @id ## @id
......
...@@ -17,10 +17,10 @@ This is a *very* simple example of a JSON schema. There is only one property. ...@@ -17,10 +17,10 @@ This is a *very* simple example of a JSON schema. There is only one property.
# Identifiable Properties # Identifiable Properties
| Property | Type | Required | Defined by | | Property | Type | Required | Nullable | Defined by |
|----------|------|----------|------------| |----------|------|----------|----------|------------|
| [@id](#id) | `string` | Optional | Identifiable (this schema) | | [@id](#id) | `string` | Optional | No | Identifiable (this schema) |
| `*` | any | Additional | this schema *allows* additional properties | | `*` | any | Additional | Yes | this schema *allows* additional properties |
## @id ## @id
......
...@@ -17,10 +17,10 @@ This is a *very* simple example of a JSON schema. There is only one property. ...@@ -17,10 +17,10 @@ This is a *very* simple example of a JSON schema. There is only one property.
# Simple Properties # Simple Properties
| Property | Type | Required | Defined by | | Property | Type | Required | Nullable | Defined by |
|----------|------|----------|------------| |----------|------|----------|----------|------------|
| [id](#id) | `string` | Optional | Simple (this schema) | | [id](#id) | `string` | Optional | No | Simple (this schema) |
| `*` | any | Additional | this schema *allows* additional properties | | `*` | any | Additional | Yes | this schema *allows* additional properties |
## id ## id
......
...@@ -17,26 +17,26 @@ This is an example schema with examples for multiple types and their constraints ...@@ -17,26 +17,26 @@ This is an example schema with examples for multiple types and their constraints
# Simple Types Properties # Simple Types Properties
| Property | Type | Required | Defined by | | Property | Type | Required | Nullable | Defined by |
|----------|------|----------|------------| |----------|------|----------|----------|------------|
| [integer_threes](#integer_threes) | `integer` | Optional | Simple Types (this schema) | | [integer_threes](#integer_threes) | `integer` | Optional | No | Simple Types (this schema) |
| [interger_constrained](#interger_constrained) | `integer` | Optional | Simple Types (this schema) | | [interger_constrained](#interger_constrained) | `integer` | Optional | No | Simple Types (this schema) |
| [interger_unconstrained](#interger_unconstrained) | `integer` | Optional | Simple Types (this schema) | | [interger_unconstrained](#interger_unconstrained) | `integer` | Optional | No | Simple Types (this schema) |
| [number_constrained](#number_constrained) | `number` | Optional | Simple Types (this schema) | | [number_constrained](#number_constrained) | `number` | Optional | No | Simple Types (this schema) |
| [number_unconstrained](#number_unconstrained) | `number` | Optional | Simple Types (this schema) | | [number_unconstrained](#number_unconstrained) | `number` | Optional | No | Simple Types (this schema) |
| [string_date](#string_date) | `string` | Optional | Simple Types (this schema) | | [string_date](#string_date) | `string` | Optional | No | Simple Types (this schema) |
| [string_email](#string_email) | `string` | Optional | Simple Types (this schema) | | [string_email](#string_email) | `string` | Optional | No | Simple Types (this schema) |
| [string_hostname](#string_hostname) | `string` | Optional | Simple Types (this schema) | | [string_hostname](#string_hostname) | `string` | Optional | No | Simple Types (this schema) |
| [string_ipv4](#string_ipv4) | `string` | Optional | Simple Types (this schema) | | [string_ipv4](#string_ipv4) | `string` | Optional | No | Simple Types (this schema) |
| [string_ipv6](#string_ipv6) | `string` | Optional | Simple Types (this schema) | | [string_ipv6](#string_ipv6) | `string` | Optional | No | Simple Types (this schema) |
| [string_length](#string_length) | `string` | Optional | Simple Types (this schema) | | [string_length](#string_length) | `string` | Optional | No | Simple Types (this schema) |
| [string_pattern](#string_pattern) | `string` | Optional | Simple Types (this schema) | | [string_pattern](#string_pattern) | `string` | Optional | No | Simple Types (this schema) |
| [string_pattern_noexample](#string_pattern_noexample) | `string` | Optional | Simple Types (this schema) | | [string_pattern_noexample](#string_pattern_noexample) | `string` | Optional | No | Simple Types (this schema) |
| [string_pattern_singleexample](#string_pattern_singleexample) | `string` | Optional | Simple Types (this schema) | | [string_pattern_singleexample](#string_pattern_singleexample) | `string` | Optional | No | Simple Types (this schema) |
| [string_unconstrained](#string_unconstrained) | `string` | Optional | Simple Types (this schema) | | [string_unconstrained](#string_unconstrained) | `string` | Optional | No | Simple Types (this schema) |
| [string_uri](#string_uri) | `string` | Optional | Simple Types (this schema) | | [string_uri](#string_uri) | `string` | Optional | No | Simple Types (this schema) |
| [yesno](#yesno) | `boolean` | **Required** | Simple Types (this schema) | | [yesno](#yesno) | `boolean` | **Required** | No | Simple Types (this schema) |
| `*` | any | Additional | this schema *allows* additional properties | | `*` | any | Additional | Yes | this schema *allows* additional properties |
## integer_threes ## integer_threes
......
...@@ -17,10 +17,10 @@ This is a schema which is currently in the `stabilizing` status. ...@@ -17,10 +17,10 @@ This is a schema which is currently in the `stabilizing` status.
# Stabilizing Properties # Stabilizing Properties
| Property | Type | Required | Defined by | | Property | Type | Required | Nullable | Defined by |
|----------|------|----------|------------| |----------|------|----------|----------|------------|
| [id](#id) | `string` | Optional | Stabilizing (this schema) | | [id](#id) | `string` | Optional | No | Stabilizing (this schema) |
| `*` | any | Additional | this schema *allows* additional properties | | `*` | any | Additional | Yes | this schema *allows* additional properties |
## id ## id
......
---
template: reference
foo: bar
---
# Type Arrays Schema
```
https://example.com/schemas/typearrays
```
This schema test type arrays and nullable types
| [Abstract](../abstract.md) | Extensible | [Status](../status.md) | Identifiable | Custom Properties | Additional Properties | Defined In |
|----------------------------|------------|------------------------|--------------|-------------------|-----------------------|------------|
| Can be instantiated | No | Experimental | No | Forbidden | Permitted | [typearrays.schema.json](typearrays.schema.json) |
# Type Arrays Properties
| Property | Type | Required | Nullable | Defined by |
|----------|------|----------|----------|------------|
| [null](#null) | `null` | Optional | No | Type Arrays (this schema) |
| [string-or-null](#string-or-null) | `string` | Optional | Yes | Type Arrays (this schema) |
| [string-or-number](#string-or-number) | multiple | Optional | No | Type Arrays (this schema) |
| [string-or-number-null](#string-or-number-null) | multiple | Optional | Yes | Type Arrays (this schema) |
| `*` | any | Additional | Yes | this schema *allows* additional properties |
## null
This is just nothing
`null`
* is optional
* type: `null`
* defined in this schema
### null Type
`null`
This property can only have the value `null`.
## string-or-null
Nullable string
`string-or-null`
* is optional
* type: `string`
* defined in this schema
### string-or-null Type
`string`, nullable
## string-or-number
Types can be many things
`string-or-number`
* is optional
* type: multiple
* defined in this schema
### string-or-number Type
Either one of:
* `string`
* `number`
## string-or-number-null
Types can be many things, even nothing at all.
`string-or-number-null`
* is optional
* type: multiple
* defined in this schema
### string-or-number-null Type
Either one of:
* `string`
* `number`
* or `null`
{
"meta:license": [
"Copyright 2017 Adobe Systems Incorporated. All rights reserved.",
"This file is licensed to you under the Apache License, Version 2.0 (the 'License');",
"you may not use this file except in compliance with the License. You may obtain a copy",
"of the License at http://www.apache.org/licenses/LICENSE-2.0"
],
"$schema": "http://json-schema.org/draft-06/schema#",
"$id": "https://example.com/schemas/typearrays",
"title": "Type Arrays",
"description": "This schema test type arrays and nullable types",
"type": "object",
"allOf": [
{
"$ref": "#/definitions/id"
}
],
"properties": {
"null": {
"type": "null",
"description": "This is just nothing"
},
"string-or-null": {
"type": [
"string",
"null"
],
"description": "Nullable string"
},
"string-or-number": {
"type": [
"string",
"number"
],
"description": "Types can be many things"
},
"string-or-number-null": {
"type": [
"string",
"number",
"null"
],
"description": "Types can be many things, even nothing at all."
}
}
}
\ No newline at end of file
{
"meta:license": [
"Copyright 2017 Adobe Systems Incorporated. All rights reserved.",
"This file is licensed to you under the Apache License, Version 2.0 (the 'License');",
"you may not use this file except in compliance with the License. You may obtain a copy",
"of the License at http://www.apache.org/licenses/LICENSE-2.0"
],
"$schema": "http://json-schema.org/draft-06/schema#",
"$id": "https://example.com/schemas/typearrays",
"title": "Type Arrays",
"description": "This schema test type arrays and nullable types",
"type": "object",
"allOf": [
{
"$ref": "#/definitions/id"
}
],
"properties": {
"null": {
"type": "null",
"description": "This is just nothing"
},
"string-or-null": {
"type": ["string", "null"],
"description": "Nullable string"
},
"string-or-number": {
"type": ["string", "number"],
"description": "Types can be many things"
},
"string-or-number-null": {
"type": ["string", "number", "null"],
"description": "Types can be many things, even nothing at all."
}
}
}
\ No newline at end of file
...@@ -103,6 +103,8 @@ function simpletype(prop) { ...@@ -103,6 +103,8 @@ function simpletype(prop) {
prop.simpletype = '`integer`'; prop.simpletype = '`integer`';
} else if (type==='object') { } else if (type==='object') {
prop.simpletype = '`object`'; prop.simpletype = '`object`';
} else if (type==='null') {
prop.simpletype = '`null`';
} else if (type==='array') { } else if (type==='array') {
if (prop.items!==undefined) { if (prop.items!==undefined) {
const innertype = simpletype(prop.items); const innertype = simpletype(prop.items);
...@@ -115,6 +117,21 @@ function simpletype(prop) { ...@@ -115,6 +117,21 @@ function simpletype(prop) {
} else { } else {
prop.simpletype = '`array`'; prop.simpletype = '`array`';
} }
} else if (Array.isArray(type)) {
function nullfilter(str) {
return str!=='null';
}
var filtered = type.filter(nullfilter);
if (type.length - 1 === filtered.length) {
prop.nullable = true;
}
if (filtered.length===1) {
prop.type = filtered[0];
prop.simpletype = '`' + filtered[0] + '`';
} else {
prop.type = filtered;
prop.simpletype = 'multiple';
}
} else { } else {
prop.simpletype = 'complex'; prop.simpletype = 'complex';
} }
......
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
* [Simple](./simple.schema.md)`https://example.com/schemas/simple` (Unknown) * [Simple](./simple.schema.md)`https://example.com/schemas/simple` (Unknown)
* [Simple Types](./simpletypes.schema.md)`https://example.com/schemas/simpletypes` (Unknown) * [Simple Types](./simpletypes.schema.md)`https://example.com/schemas/simpletypes` (Unknown)
* [Stabilizing](./stabilizing.schema.md)`https://example.com/schemas/stabilizing` (Stabilizing) * [Stabilizing](./stabilizing.schema.md)`https://example.com/schemas/stabilizing` (Stabilizing)
* [Type Arrays](./typearrays.schema.md)`https://example.com/schemas/typearrays` (Unknown)
## /subdir/ ## /subdir/
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment