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

mutual $data reference with default does not seem to work #49

Open
fakraemer opened this issue Feb 6, 2015 · 1 comment
Open

mutual $data reference with default does not seem to work #49

fakraemer opened this issue Feb 6, 2015 · 1 comment

Comments

@fakraemer
Copy link

This is a schema fragment I've written

"cpu" : {
  "additionalProperties": false,
  "properties": {
    "low": {
      "description": "The low threshold for the CPU utilization alarm.",
      "type": "integer",
      "default": 45,
      "minimum": 0,
      "maximum": { "$data": "1/high" }
    },
    "high": {
      "description": "The high threshold for the CPU utilization alarm.",
      "type": "integer",
      "default": 70,
      "minimum": { "$data": "1/low" },
      "maximum": 100
    }
  }
}

and I use the validator like this:

validator.addSchema('descriptor', JSON.parse(fs.readFileSync('./schema/schema.json')));
var errors = validator.validate('descriptor', opts.descriptor, {useDefault: true});

when I invoke this with both values set in the descriptor, the boundary checks work as expected. If I set e.g. only low to 80 (exceeding high) I would have expected the validation to fail but it passes. Am I using this incorrectly because it's a mutual reference or is the default not applied before this validation happens?

@cristiano-belloni
Copy link

+1

Even when it's not mutual, it does not work.
Seems default is not applied before the validation happens. This is very inconvenient.

The workaround I found for this is to validate with useDefault and, if the validation does not get any errors, revalidate the same object and return the result.

The first pass will apply the defaults to the passed object and the second pass will validate a different object (the one containing defaults) against the validation.

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

No branches or pull requests

2 participants