0

I am trying to fix property values when they have different value than specified.

JSON:

{
    "stone": "bri"
}

I want to fix the stone property to have "brick" if it is not already "brick".

Schema:

{
    "required": [
        "stone"
    ],
    "properties": {
        "stone": {
            "type": "string",
            "if": { 
                "not": {
                    "constant": "brick"
                }
            },
            "then": { "default": "brick" }
        }
    }
}

Using v4.11.4:

var ajv = new Ajv({ useDefaults: true, v5: true });
require('ajv-keywords')(ajv);

Is there any other keyword that I can use to basically change the value to the good one, or to an other value by reference?

Z T
  • 543
  • 1
  • 7
  • 26

1 Answers1

2

Solved with a custom keyword:

ajv.addKeyword('modify_current', {
    modifying: true,
    validate: function (schema_parameter_value, validated_parameter_value, validation_schema_object, current_data_path, validated_parameter_object, validated_parameter) {
        validated_parameter_object[validated_parameter] = schema_parameter_value;
        return true;
    },
    errors: false
});

And in the schema:

"then": { "modify_current": "brick" }
Z T
  • 543
  • 1
  • 7
  • 26