Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If/then/else doesn't seem to work correctly #770

Closed
markcmueller opened this issue Apr 26, 2018 · 7 comments
Closed

If/then/else doesn't seem to work correctly #770

markcmueller opened this issue Apr 26, 2018 · 7 comments
Labels

Comments

@markcmueller
Copy link

markcmueller commented Apr 26, 2018

What version of Ajv are you using? Does the issue happen if you use the latest version?
6.4.0

Ajv options object

JSON Schema

{
  "id": "urn:json-schema:com.demo.Demo",
  "title": "Demo",
  "description": "A demo of if/then/else not working",
  "type": "object",
  "properties": {
    "method": {
      "enum": ["AA", "BB", "CC"]
    },
    "name": {
      "description": "Name of the product",
      "type": "string",
      "pattern": "^[a-zA-Z]{2}$"
    },
    "price": {
      "type": "string",
      "pattern": "^[0-9]{2}$"
    }
  },
  "allOf": [{
    "if": {
      "properties": {
        "method": {
          "enum": ["AA"]
        }
      }
    },
    "then": {
      "required": ["method", "name", "price"]
    },
    "else": {
      "required": ["method", "name"]
    }
  }]
}

Sample data

{
  "method": "AA",
  "product": "zs"
}

Your code

  validateAgainstSchema() {
    const jsonString = this.state.value;
    const schema = require('./data/demo-schema.json');

    var Ajv = require('ajv');
    var ajv = new Ajv(); // options can be passed, e.g. {allErrors: true}
    ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-07.json'));
    var validate = ajv.compile(schema);

    let json;
    try {
      json = JSON.parse(jsonString);
    } catch (err) {
      alert('Invalid json: ' + err);
    }
    if ( json != null ) {
      var valid = validate(json);
      if (!valid) console.log(validate.errors);

      const message = (valid ? "It's good!" : "Not good: " + validate.errors);
      alert('Validation: ' + message);
    }
  }
}

Validation result, data AFTER validation, error messages

Validation: It's good!

What results did you expect?
expected error: required key [price] not found
This is what the java library, everit-org/json-schema, returns.

Are you going to resolve the issue?
I don't have the expertise.

@epoberezkin
Copy link
Member

epoberezkin commented Apr 26, 2018

See https://runkit.com/esp/5ae23d12d0320b001260cdb3

You must be running some old version that doesn't have if/then/else, as otherwise explicitly adding draft-07 meta-schema would throw an exception.

Also "id" should be "$id" in draft-07

@markcmueller
Copy link
Author

markcmueller commented Apr 27, 2018 via email

@markcmueller
Copy link
Author

markcmueller commented Apr 27, 2018 via email

@epoberezkin
Copy link
Member

I suspect it may be related to #708, which is npm issue with sub-dependencies. Maybe using yarn will help?

@epoberezkin
Copy link
Member

Try this:

require('ajv');
console.log(Object.keys(require.cache).filter(s => s.includes('ajv')))

After that you can check the version of Ajv in package.json in the folder where you get it from.

@epoberezkin
Copy link
Member

Also, look at the full npm ls output - there can be two versions...

@markcmueller
Copy link
Author

markcmueller commented Apr 30, 2018 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants