mafintosh / is-my-json-valid

A JSONSchema validator that uses code generation to be extremely fast
MIT License
963 stars 111 forks source link

filter() constructor doesn't seem to show errors. #44

Open dkran opened 9 years ago

dkran commented 9 years ago

I have a small validator written as such:

var vState = validator.filter({
  required: true,
  type: 'object',
  properties: {
    state_iso: {
      required: true,
      type: 'string'
    },
    state_name: {
      required: true,
      type: 'string'
    },
    fips_code: {
      required: false,
      type: 'number'
    }
  },
  additionalProperties: false
});

Long story short, if I take off the .filter, it will throw errors if I take off required fields and the like, but also throw errors on fields that need to be stripped. if I put filter on, it seems the validator won't pass, but the errors array is always undefined.
simple route logic right now just to test this is:

console.log(req.body)
  vState(req.body)
  console.log(req.body)
  console.log(vState.errors)
  res.end()

my actual code is commented out

jonasfj commented 9 years ago

I've run into the same issue... It's a problem with the generated code giving options {filter: true} to validator doesn't work either. Ie. validator(schema, {filter: true, verbose: true}), it just ignores any type errors.

As a temporary work around I'm doing:

var validator = require('is-my-json-valid')
var filter    = validator.filter(schema);
var validate  = validator(schema, {
  verbose:    true,
  greedy:     true
});

var filterAndValidate = function(data) {
  filter(data);
  validate(data);
  console.log(validate.errors);
};

It's obviously slower, so not a fix... Just including it as context.

dkran commented 9 years ago

I have something similar going on as a workaround. I also started poking around at the imjv source to see if I could trace it back, but it's a bit confusing to me.

dkran commented 9 years ago

I wish this was getting a little attention here.