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

Default value is not validated correctly against array type #196

Closed
greyblake opened this issue Aug 9, 2018 · 2 comments
Closed

Default value is not validated correctly against array type #196

greyblake opened this issue Aug 9, 2018 · 2 comments

Comments

@greyblake
Copy link

Hi!
Thanks for the cool library!

I got some problems initializing a schema with fields that may have multiple types. Here is a minimal example to reproduce:

var Type = require('avsc').Type;

var schemeDefinition =
{
  "type": "record",
  "namespace": "xyz",
  "name": "failureExample",
  "version": 1,
  "fields": [
    {
      "name": "factor",
      "type": ["null", "string"],
      "default": "23",
      "logicalType": "standardDecimal"
    }
  ]
}

var schema = Type.forSchema(schemeDefinition);

Exception:

/node_modules/avsc/lib/types.js:3003
  throw new Error(f('invalid %s: %j', type, val));
  ^

Error: invalid "null": "23"

As I figured it out, the library tried to validate default value "23" against the first type in the type array. However the first type is null, so it says that "23" is not a valid null. Which is correct.

Expected behaviour

The library tries to validate the default value against all the types listed in the type array. And spits the exception only when no passing type is found.

Work around

So far I've found the following workound. If I change the order of elements in the type array and put on the first place a type that represents the default value it works.

So changing this

      "type": ["null", "string"],

To this

      "type": ["string", "null"],

Allows to avoid the error.

Thanks!

@mtth
Copy link
Owner

mtth commented Aug 10, 2018

Hi @greyblake, thanks for the detailed report! This is actually WAI; from the Avro specification (emphasis mine):

default: A default value for this field, used when reading instances that lack this field (optional). Permitted values depend on the field's schema type, according to the table below. Default values for union fields correspond to the first schema in the union. Default values for bytes and fixed fields are JSON strings, where Unicode code points 0-255 are mapped to unsigned 8-bit byte values 0-255.

So your workaround is actually what the specification says you should do...

@mtth mtth closed this as completed Aug 10, 2018
@greyblake
Copy link
Author

@mtth Thanks for the quick reply!
The link to the specification you've shared makes sense. It means that we're trying to use invalid schema :)

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