GithubHelp home page GithubHelp logo

Comments (6)

Marak avatar Marak commented on June 18, 2024

What is a union properties? I don't understand what you are trying to say.

from revalidator.

pksunkara avatar pksunkara commented on June 18, 2024

Is it this?

{"type":["string","number"]}

The property can be either string or number?

from revalidator.

ggoodman avatar ggoodman commented on June 18, 2024

Hi PK, Marak, sorry about the vague bug report. Here is some more context.

Consider a Post that can optionally have a source which is defined by a schema itself. Basically, I want to validate some json such that the source property is either not present (null) or conforms to a schema. To achieve this, I referred to section 5.1 of the draft.

Here is an example json-schema (v3) to describe a Post:

{
  "type": "object",
  "properties": {
    "title": {
      "type": "string",
      "required": true
    },
    "body": {
      "type": "string",
      "required": true
    },
    "source": {
      "type": [
        {
          "type": "null"
        }, {
          "type": "object",
          "properties": {
            "name": {
              "type": "string",
              "required": true
            },
            "url": {
              "type": "string",
              "required": true
            }
          }
       ]
    }
  }
}

from revalidator.

Marak avatar Marak commented on June 18, 2024

I see. If you need a 100% compliant validator, you might want to check out https://github.com/garycourt/JSV

from revalidator.

ggoodman avatar ggoodman commented on June 18, 2024

Hi Marak, this is what I'm using and seems to be more or less API compatible: https://github.com/kriszyp/json-schema/blob/master/lib/validate.js

from revalidator.

elad avatar elad commented on June 18, 2024

I think this is already supported. Here's some code:

var revalidator = require('revalidator');

var schema = {
    properties: {
        title: {
            type: 'string',
            required: true
        },

        body: {
            type: 'string',
            required: true
        },

        source: {
            type: ['object', 'null'],
            properties: {
                name: {
                    type: 'string',
                    required: true
                },
                url: {
                    type: 'string',
                    required: true
                }
            }
        }
    }
};

// good: missing, null, or conforming to schema.
var object1 = {
    title: 'a title',
    body: 'this is the body'
}, object2 = {
    title: 'a title',
    body: 'this is the body',
    source: null
}, object3 = {
    title: 'a title',
    body: 'this is the body',
    source: {
        name: 'foo bar',
        url: 'http://www.example.com'
    }
};

// bad: empty, not conforming to schema.
var object4 = {
    title: 'a title',
    body: 'this is the body',
    source: {}
}, object5 = {
    title: 'a title',
    body: 'this is the body',
    source: {
        name: 'foo bar',
        a: 1337
    }
}, object6 = {
    title: 'a title',
    body: 'this is the body',
    source: {
        a: 1337,
        url: 'http://www.example.com'
    }
};

console.log(revalidator.validate(object1, schema));
console.log(revalidator.validate(object2, schema));
console.log(revalidator.validate(object3, schema));
console.log(revalidator.validate(object4, schema));
console.log(revalidator.validate(object5, schema));
console.log(revalidator.validate(object6, schema));

Output:

$ node opt
{ valid: true, errors: [] }
{ valid: true, errors: [] }
{ valid: true, errors: [] }
{ valid: false,
  errors: 
   [ { attribute: 'required',
       property: 'source.name',
       expected: true,
       actual: undefined,
       message: 'is required' },
     { attribute: 'required',
       property: 'source.url',
       expected: true,
       actual: undefined,
       message: 'is required' } ] }
{ valid: false,
  errors: 
   [ { attribute: 'required',
       property: 'source.url',
       expected: true,
       actual: undefined,
       message: 'is required' } ] }
{ valid: false,
  errors: 
   [ { attribute: 'required',
       property: 'source.name',
       expected: true,
       actual: undefined,
       message: 'is required' } ] }
$ 

I think this issue can be closed as well.

from revalidator.

Related Issues (20)

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.