Skip to content

Non-mutating useDefaults option #549

@danny-andrews

Description

@danny-andrews

What version of Ajv you are you using?
5.2.2

What problem do you want to solve?
I want a non-mutating version of the useDefaults option.

What do you think is the correct solution to problem?
I think that instead of mutating the passed in data, we could set a property on the ajv object with the values, similar to how we do it with errors. Example:

var ajv = new Ajv({ useDefaults: true });
var schema = {
  "type": "object",
  "properties": {
    "foo": { "type": "number" },
    "bar": { "type": "string", "default": "baz" }
  },
  "required": [ "foo", "bar" ]
};

var data = { "foo": 1 };

var validate = ajv.compile(schema);

console.log(validate(data)); // true
console.log(data); // { "foo": 1 } (unchanged)
console.log(ajv.result); // { "foo": 1, "bar": "baz" }

Note: this technique could be applied to filtering and coercing data types as well.

Will you be able to implement it?
Sure thing. With some help understanding doT and why it's used for source code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions