Given the schema
below, how would I access the minLength
property to dynamically show the user how many characters are required?
export const schema = {
type: 'object',
properties: {
inputControl: {
type: 'string',
format: 'email',
minLength: 10,
errorMessage: {
minLength: 'String must contain at least ${1/minLength} character(s)',
format: 'Please enter valid email',
}
},
},
}
I have tried ${1/minLength}
and numerous other options, but everything returns undefined
or throws an error. The best I got was ${0#}
, which returns inputControl
.
From your examples, 1/minLength
and 0#
, it looks like you're trying to use https://json-schema.org/draft/2020-12/draft-bhutton-relative-json-pointer-00, which is derived from JSON Pointer.
Relative JSON Pointer, e.g. 2#/foo/bar
, allows for a numeric value, 2
, in the front to indicate the number of parent levels to travel up the JSON structure prior to evaluating the ordinary pointer, /foo/bar
. As you can imagine, this also requires a starting node from which to evaluate the pointer.
A JSON Pointer, on the other hand, always evaluates from the root of the structure. It must always start with a slash. (If it's URI-encoded, it would be part of the URI fragment, so it needs to follow a #
to distinguish it from the URI path.)
The JSON Pointer that gets you to the minLength
value for the JSON (doesn't have to be a schema) you posted is
/properties/inputControl/minLength
However, for your use case, I'd probably just explicitly include 10
in the error message. The value is explicitly listed in the subschema anyway. Dynamically generating that error message isn't providing any benefit since it will always generate the same value.
As a note, errorMessages
isn't a standard JSON Schema keyword, which means interoperable support for it isn't guaranteed.