Easy way to resolve JSON schema conditions in JS?

Viewed 23

Say I have the following JSON schema:

const oneOfConditional = [
      {
        if: {
          properties: {
            type: {
              enum: ["numerical"],
            },
          },
        },
        then: {
          properties: {
            op: {
              default: "=",
              enum: ["=", "<", ">", "<=", ">="],
              metadata: {
                labels: [
                  "is equal to",
                  "is less than",
                  "is greater than",
                  "is less than or equal to",
                  "is greater than or equal to",
                ],
              },
            },
          },
        },
      },
    ]

const allOfConditional: [
      {
        if: {
          properties: {
            op: {
              enum: ["=", "!="],
            },
            type: {
              enum: ["categorical"],
            },
          },
        },
        then: {
          properties: {
            value: {
              type: "array",
              items: {
                type: "string",
              },
            },
          },
        },
        else: {
          properties: {
            value: {
              type: ["number", "string"],
            },
          },
        },
      },
      {
        if: {
          properties: {
            type: {
              enum: ["datetime"],
            },
          },
        },
        then: {
          properties: {
            format: {
              type: "string",
              default: "YYYY-MM-DD",
            },
          },
        },
      },
    ]

const complexSchema = {
  title: "Filters",
  type: "array",
  minItems: 0,
  maxItems: 100,
  items: {
    type: "object",
    required: ["column", "type"],
    oneOf: oneOfConditional,
    allOf: allOfConditional,
    properties: {
      type: {
        title: "Type",
        type: "string",
        enum: ["categorical", "datetime", "string", "numerical"],
      },
      op: {
        title: "Operator",
        type: "string",
      },
      conj: {
        title: "Conjunction",
        type: "string",
        default: "AND",
        enum: ["OR", "AND"],
        
      },
      value: {
        title: "value",
     
      },
    },
  },
}

For the schema above, I have the following data to be validated on it:

[{type:"numerical", op:"", conj:"AND",value:null}]

I'm using AJV for validation. I don't just want the resulting errors from the validation though. I also want the same schema, but with the conditionals resolved within the schema. So, I would expect the data above to resolve the schema to the following:

{
  title: "Filters",
  type: "array",
  minItems: 0,
  maxItems: 100,
  items: {
    items: {
      type: "object",
      required: ["column", "type"],
      properties: {
        type: {
          title: "Type",
          type: "string",
          enum: ["categorical", "datetime", "string", "numerical"],
        },
        op: {
          default: "=",
          enum: ["=", "<", ">", "<=", ">="],
          metadata: {
            labels: [
              "is equal to",
              "is less than",
              "is greater than",
              "is less than or equal to",
              "is greater than or equal to",
            ],
          },
        },
        conj: {
          title: "Conjunction",
          type: "string",
          default: "AND",
          enum: ["OR", "AND"],
        },
        value: {
          title: "value",
          type: ["number", "string"],
        },
      },
    },
  },
}

Anyone know of a solution to this?

0 Answers
Related