Code Monkey home page Code Monkey logo

json-schema-instantiator's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

json-schema-instantiator's Issues

Support for array of types

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!

No support of allOf

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",
        }
      }
    }
  ]
}

Add options to instantiate method to control instance construction

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.

Using default value of outer definition

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.

Consider not instantiate array items

I guess without minItems it makes little sense to instantiate array items(!?).

Atm the moment I clear the array item,
post instantiating the schema.

add support for enums

Hi.. I have added support for enums... if you give me the permission, I'll create a pull request.

Does not support references in schema

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?

Arrays instantiated without minItems leads to a non-empty array

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!

Doesn't handle consts correctly

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"
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.