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

Issues with union in validation #37

Closed
dmtrs opened this issue Feb 19, 2016 · 2 comments
Closed

Issues with union in validation #37

dmtrs opened this issue Feb 19, 2016 · 2 comments

Comments

@dmtrs
Copy link

dmtrs commented Feb 19, 2016

I have the following simple example:

var test = require('tape');
var avsc = require('avsc');

test('should be valid', function(assert) {
  assert.plan(1);
  var schema = avsc.parse({
    type: "record",
    name: "nullable",
    fields: [
      { name: "foo", type: [ "null", "string" ] }
    ]
  });
  var valid = schema.isValid({ foo: "bar" }, { errorHook: assert.comment });
  assert.ok(valid);
});

And I was expecting that {"foo":"bar"} is valid based on the schema defined above but it seems not, because test fails with valid === false as a result of isValid call.

I am using node v4.2.3 and avsc ^3.3.11 and in order to get the above running npm install tape

@dmtrs
Copy link
Author

dmtrs commented Feb 19, 2016

I just realized that the value of "foo" should be { string: 'var' } is this instructed from the specification? is it allowed to have the { "foo": "bar" } syntax?

@mtth
Copy link
Owner

mtth commented Feb 19, 2016

This is how the spec defines JSON-encoding. It is also used to represent decoded values in order to guarantee correctness. If you can make assumptions on your schemas (for example, you only ever have unions ["null", something]), you might be able to use the representation you mention and still remain correct. See #16 for more context and examples.

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