I am currently working on creating a JSON schema to validate cross references using AJV and TypeScript.
Is there a method to verify that a property includes another property from any item within the array (not a specific item in the array)?
Both the property to be checked and the 'source' property are located within items inside object arrays.
For instance:
import Ajv, { ValidateFunction } from "ajv";
const ajv = new Ajv({$data: true, strict: true});
const schema = {
type: "object",
properties: {
things: {
type: "array",
items: {
type: "object",
properties: {
thing_name: {
type: "string"
}
}
}
},
other: {
type: "string",
enum: { $data: "0/i/dont/know/how/to/address/items/in/this"
}
}
}
const validData = {
things: [
{ thing_name: "foo" },
{ thing_name: "bar" }
],
other: "foo"
}
const invalidData = {
things: [
{ thing_name: "foo" },
{ thing_name: "bar" }
],
other: "baz"
}
const validator = ajv.compile(schema);
validator(validData) // true
validator(invalidData) // true, but should be false
Even with both validations returning true, I have struggled to create a valid JSON reference that validates correctly.
I attempted using an enum field containing:
{ $data: "0/things/thing_name" }
{ $data: "0/things/0/thing_name" },
{ $data: "0/things/items/properties/thing_name/enum" }
However, due to AJV always validating when it cannot resolve the reference, it is challenging to comprehend the inner workings.
Is there a solution for this, or should I consider implementing a 'helper script'?