Package Exports
- json-schema-to-decoders
- json-schema-to-decoders/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (json-schema-to-decoders) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
json-schema-to-decoders
A handy utility for converting JSON schema definition to decoders javascript code.
Installation
To use the CLI you can install the package globally:
npm install -g json-schema-to-decoders
Usage
To convert a JSON definition file to decoders, use the following command CLI:
json-schema-to-decoders <schema.json>
Alternatively you can use it as a library:
import Converter from "json-schema-to-decoders";
console.log(await Converter.convertFile("path/to/schema.json"));
Support Matrix
The following table summarizes the supported conversion between JSON Schema types and decoders.
Type | Validation / Keyword | Status |
---|---|---|
All Types | enum |
✅ |
const |
✅ | |
References | Basic Support | ✅ [1] |
any |
Basic Support | ✅ |
string |
Basic Support | ✅ |
minLength |
✅ | |
maxLength |
✅ | |
pattern |
✅ | |
format: "date-time |
✅ | |
format: "time |
- | |
format: "date |
- | |
format: "duration |
- | |
format: "email |
✅ | |
format: "idn-email |
- | |
format: "hostname |
✅ | |
format: "idn-hostname |
- | |
format: "ipv4 |
- | |
format: "ipv6 |
- | |
format: "uuid |
✅ | |
format: "uri |
✅ | |
format: "uri-reference |
- | |
format: "iri |
- | |
format: "iri-reference |
- | |
integer |
Basic Support | ✅ |
number |
Basic Support | ✅ |
multipleOf |
✅ | |
minimum |
✅ | |
maximum |
✅ | |
exclusiveMinimum |
✅ | |
exclusiveMaximum |
✅ | |
boolean |
Basic Support | ✅ |
null |
Basic Support | ✅ |
array |
Basic Support | ✅ |
Unevaluated Items | - | |
items |
✅ | |
prefixItems |
🟨 [2] | |
contains |
- | |
minContains |
- | |
maxContains |
- | |
minItems |
✅ | |
maxItems |
✅ | |
uniqueItems |
✅ | |
object |
Basic Support | ✅ [3] |
Unevaluated Properties | - | |
Extending Closed Schemas | - | |
properties |
✅ | |
additionalProperties |
✅ | |
required |
✅ | |
patternProperties |
✅ | |
propertyNames |
✅ | |
minProperties |
✅ | |
maxProperties |
✅ | |
Schema Composition | allOf |
✅ |
oneOf |
🟨 [4] | |
anyOf |
✅ | |
discriminator |
✅ | |
Conditional Schemas | dependentRequired |
- |
dependentSchemas |
- | |
if |
- | |
then |
- | |
else |
- |
Remarks:
[1] Implemented through a user-provided reference resolution function that returns the variable name of a previously defined decoder.
[2] Currently
prefixItems
cannot be used together withitems
. This means that declaring additional items for arrays is not supported.
[3] Note that while for
type: "object"
the JSON Schema spec indicates that "Using non-strings as keys is invalid JSON", the javascript implementation implicitly converts all properties to strings, so the decoders will always validate even numbers in the object keys.
[4] The
oneOf
is currently polyfilled with theanyOf
behaviour. This means that the "exactly one" validation is not respected.