Reputation: 40084
I am using ajv-errors with ajv v8 - According to docs I should be able to use a pointer to reference the field name but my attempts are failing with errors such as: Cannot access property/index 0 levels up, current level is 0
Example schema:
{
$schema: "http://json-schema.org/draft-07/schema#",
type: "object",
additionalProperties: false,
properties: {
name: { type: "string" }
},
required: ["name"],
errorMessage: {
required: 'Field ${0#} is so required'
}
}
Any ideas? I could probably use an absolute pointer but am not sure what data this is actually referring to in order to create a path.
Upvotes: 3
Views: 2351
Reputation: 74680
0#
is a relative json pointer for the current members name or array index.
ajv-errors
bases this lookup on the { field: value }
data it is validating rather than somewhere in the schema.
So inside a property errorMessage
you have access to an object 1 level up:
{
type: "object",
properties: {
named: {
type: "string",
errorMessage: {
type: 'Field ${0#} is so ${1}', // 'Field "named" is so {"named":2}'
},
},
},
}
It appears for required
, there is no field or value so no data to lookup.
const schema = {
type: "object",
properties: {
name: {
type: "string",
},
},
required: ["name"],
errorMessage: {
required: {
name: 'Field "name" is so ${0}', // 'Field "name" is so {}'
}
},
}
You can add an explicit message for each field as above.
Upvotes: 5