tomarad / json-schema-instantiator Goto Github PK
View Code? Open in Web Editor NEWA simple tool for instantiating JSON Schemas.
License: MIT License
A simple tool for instantiating JSON Schemas.
License: MIT License
Hi!
So in my project we recently started doing something like this:
{ type: ['string', 'null'] }
For reference: http://json-schema.org/latest/json-schema-validation.html#anchor79
5.5.2.1. Valid values
The value of this keyword MUST be either a string or an array. If it is an array, elements of the array MUST be strings and MUST be unique.
String values MUST be one of the seven primitive types defined by the core specification.
I went ahead and did a quick implementation which suits my needs, but not sure if there's a better/ more global solution.
Since mine basically defaults to the first in the array of types.
Is it worth expanding to maybe support a default value?
For example:
{ type: ['string', 'number', 'null'], default: 100 }
as long as the default was a type in the list. The type can be inferred and cross-checked against the default value.
Anyways, I've put up PR for the change that I made. In case you'd like to take a look or suggest a better way of doing it.
#13
Thanks!
allOf
keyword is not supported for objects.
For example instantiating the following schema returns undefined
:
{
"allOf": [
{
"type": "object",
"properties": {
"title": {
"type": "string",
}
}
},
{
"type": "object",
"properties": {
"example": {
"type": "string",
}
}
}
]
}
The instantiator return instances with all proprieties instantiated, but it should be interesting to add an option on instantiate method to optionally control that as follows:
var instantiator = require('json-schema-instantiator');
...
var schema = {
"type": "object",
"properties": {
"title": { "type": "string"},
"name": { "type": "string"}
},
"required": ["title"]
};
instance = instantiator.instantiate(schema, { requiredPropertiesOnly: true });
// instance === { title: "" }
instance = instantiator.instantiate(schema); // or { requiredPropertiesOnly: false }
// instance === { title: "", name: "" }
By the default the value of requiredPropertiesOnly should be false.
JSON-Schema-Instantiator/index.js
Line 28 in 2f6884c
Hi,
If a schema contains a reference to a definition, and only the outer schema has a "default" property, it is not used.
For example:
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$ref": "#/definitions/Parameters",
"definitions": {
"Parameters": {
"type": "object",
"properties": {
"enumValue": {
"$ref": "#/definitions/EnumValue",
"default": "val_b"
}
}
},
"EnumValue": {
"type": "string",
"enum": [
"val_a",
"val_b"
]
}
}
}
Should generate
{
"enumValue": "val_b"
}
But actually generates:
{
"enumValue": "val_a"
}
If the inner definition contains a default value, it is used correctly.
Thanks.
The expected property is "requiredPropertiesOnly" and not "requiredOptionsOnly" as indicated in the readme.
I guess without minItems
it makes little sense to instantiate array items(!?).
Atm the moment I clear the array item,
post instantiating the schema.
Hi.. I have added support for enums... if you give me the permission, I'll create a pull request.
Given the following:
this.model = this.instantiator.instantiate({
"id": "#",
"type": "object",
"definitions": {
"fields": {
"type": "object",
"properties": {
"title": {
"type": "string",
"default": "Example"
},
"description": {
"type": "string"
}
}
}
},
"allOf": {
$ref: "#/definitions/fields"
},
"required": ["title"]
}, {requiredPropertiesOnly: false});
The resulting instance is undefined. Works without the $ref, but this is central to many of our schemas. It appears support for $ref is lacking presently?
I've recently ran into this issue where instantiating an array lead to non-empty arrays being created.
According to the spec, omitting the minItems should be treated as a minItems: 0
.
Currently this is what you would get.
Example Schema
{
"title": "todo",
"type": "object",
"properties": {
"todos": {
"type": "array",
"items": {
"type": "integer"
}
}
},
"required": ["todos"]
};
Expected
{
'todos': []
}
Actual
{
'todos': [0]
}
I've got a feature branch in my fork with an accompanying test. Will open a PR shortly, but wanted to post here in case there was a reason for this behavior.
Thanks!
Given the following schema:
{
"$id": "entity/user_adult",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"required": ["type"],
"properties": {
"type": { "const": "user_adult" }
}
}
You get an empty object, when it should have:
{
"type": "user"
}
Need enum support for my project so I manually updated to v0.4.1.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.