Closed mark007 closed 7 years ago
ajv wouldn't help your case, it is only for validation, it's also written in a template language, but also it wouldn't work on a $ref object.
$ref overrides the entire object, to quote RFC Section 7 The value of the $ref is a URI Reference. Resolved against the current URI base, it identifies the URI of a schema to use. All other properties in a "$ref" object MUST be ignored.
There is a separate proposal for $patch and $merge keywords to do what you are asking, but they haven't made it into Draft 6, so not sure when or if they will be approved or if something else will take their place.
When json-schema determines their ultimate solution I will no doubt try to implement it. I am already planning on making it possible to force values to reset to default should they be hidden or meet criteria.
Please re-open if you do not feel the question has been answered sufficiently or have more to add :)
OK no problem, using useDefaults: true in Ajv options does in fact work though and it does populate the default in that case. Maybe its as you say not following the spec in that case as $ref and default, does actually populate the default into the data during the validate.
Although in my example above the default, is not a property of $ref itself, its a property of "router_name" so maybe it is valid.
@mark007 you'll find it wont work in ajv past version five as he is defaulting back to the standard, additional flags will be needed to indicate you want to bypass the standard. ajv also supports proposed keywords that are not accepted yet, so it is worth reading up on what is standard before adding anything that library validates that may not be supported in any other library.
Enhancement
As a user/developer, when I have a property that uses both a $ref aswell as a default I should be able to see the default value appear on the UI
Expected behaviour
I expected the default value to appear on the UI
Actual behaviour
It actually didn't show the default value
Gist/Plunker/Demo
I can't reproduce on the online example / gist as the field doesn't actually appear for me at all in that case. I'm locally using the latest angular-schema-form-bootstrap commit as of this very moment.
Basically if a field has something like this, the default isn't used. Tools like ajv can make use of the default however.
"properties": { "router_name": { "$ref": "#/definitions/mydef", "default": "asdf" } } }
Could something like ajv be potentially used, to populate the defaults (https://github.com/epoberezkin/ajv#assigning-defaults) in the data before being passed to angular schema form. Would that make angular schema forms life easier if it could reuse something like ajv for certain things like this before presentation.
@json-schema-form/angular-schema-form-lead