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 values inside referenced schemas will not fill default on validation #688

Closed
filipesilva opened this issue Jan 26, 2018 · 5 comments

Comments

@filipesilva
Copy link

filipesilva commented Jan 26, 2018

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

Ajv options object

{ useDefaults: true }

JSON Schema

var schema = {
  "$async": true,
  "$id": "http://example.com/schemas/schema.json",
  "type": "object",
  "properties": {
    "foo": { "$ref": "defs.json#/definitions/defaultStr" },
    "bar": { "$ref": "defs.json#/definitions/str" },
    "baz": { "type": "string", "default": "baz" }
  }
};

var defsSchema = {
  "$id": "http://example.com/schemas/defs.json",
  "definitions": {
    "defaultStr": { "type": "string", "default": "foo" },
    "str": { "type": "string" }
  }
};

Sample data

{"bar": "bar"}

Your code

I also have a runkit here: https://runkit.com/filipesilva/5a6b8e108f3ceb0012877755

const Ajv = require("ajv");

var schema = {
  "$async": true,
  "$id": "http://example.com/schemas/schema.json",
  "type": "object",
  "properties": {
    "foo": { "$ref": "defs.json#/definitions/defaultStr" },
    "bar": { "$ref": "defs.json#/definitions/str" },
    "baz": { "type": "string", "default": "baz" }
  }
};

var defsSchema = {
  "$id": "http://example.com/schemas/defs.json",
  "definitions": {
    "defaultStr": { "type": "string", "default": "foo" },
    "str": { "type": "string" }
  }
};

var ajv = new Ajv({ useDefaults: true });
ajv.addSchema(defsSchema);
var validate = ajv.compile(schema);
test({bar: 'bar'})

function test(data) {
    return validate(data)
    .then(() => console.log('valid data: ' + JSON.stringify(data)))
    .catch((err) => {
        if (err instanceof Ajv.ValidationError)
            console.log('invalid data: ' + JSON.stringify(data), 'errors:', err.errors);
        else console.log(err);
    });
}

Validation result, data AFTER validation, error messages

{"bar":"bar","baz":"baz"}

What results did you expect?
I expected the default for foo, specified in defs.json, to be filled in like baz was.

{"foo": "foo","bar":"bar","baz":"baz"}

Are you going to resolve the issue?
I am not sure if this is a bug or intended behaviour.

In https://github.com/epoberezkin/ajv#assigning-defaults the following caveat is listed:

default keywords in other cases are ignored: ... in schemas generated by custom macro keywords

But I don't really know if using $ref falls within that category.

@filipesilva
Copy link
Author

filipesilva commented Jan 26, 2018

This seems similar to defaults also not working with merge (https://runkit.com/filipesilva/5a6b98558f3ceb0012877ec2), but that is reported in ajv-validator/ajv-merge-patch#16 and is marked as a future enhancement.

@filipesilva
Copy link
Author

I was trying $ref within the same document and it seems that doesn't fill in defaults as well: https://runkit.com/filipesilva/5a6b9bbd42f34400128620eb

@epoberezkin
Copy link
Member

duplicate of #337

@filipesilva
Copy link
Author

Aw jeez, I didn't see that one. Thanks for directing me to the right issue!

@epoberezkin
Copy link
Member

no problem at all

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

No branches or pull requests

2 participants