1

I'm trying to validate the bellow json with ajv but i'm running into problems with validating the inputs options because the validation change based on the inputs type.

I found this post which was very helpful but im still a bit stuck

My json:

  {
        "name": "test Template",
        "canvasSize": {"width": 2480, "height": 3508},
        "inputs": [
            {
                "hidden": true,
                "name": "Background",
                "type": "background",
                "position": {"x": "0", "y": 0},
                "value": "http://localhost:3000/Featurecompactv2.jpg",
                "options": {}
            },                {
                    "name": "Title Line",
                    "group": "title",
                    "type": "text",
                    "position": {"x": "center", "y": 1400},
                    "options": {
                        "fontSize": 240,
                        "font": "Arial Black",
                        "color": "black",
                        "textAlign": "center",
                        "textBaseline": "alphabetic",
                        "direction": "inherit"
                    }
                
    
                },
                {
                    "name": "Image",
                    "type": "image",
                    "position": {"x": "center", "y": 2200},
                    "options": {
                        "maxWidth": 1000,
                        "maxHeight": 1000
                    }
                },
    
                {
                    "name": "Barcode",
                    "type": "barcode",
    
                    "position": {"x": 2100, "y": 2800},
                    "options": {
                        "width": 4,
                        "height": 35,
                        "fontSize": 35
    
                    }
                }
        ]
    }

My schema so far:


const positionSchema = {
  type: "object",
  properties: {
    x: { oneOf: [{ type: "number" }, { enum: ["center"] }] },
    y: { oneOf: [{ type: "number" }, { enum: ["center"] }] },
  },
};

const textOptionsSchema = {
  type: "object",
  properties: {
    fontSize: { type: "number" },
    font: { type: "string" },
    color: { type: "string" },
    textAlign: { type: "string" },
    textBaseline: { type: "string" },
    direction: { type: "string" },
    prefix: { type: "string" },
    disabled: { type: "boolean" },
  },
  additionalProperties: false,
};

const barcodeOptionsSchema = {
  type: "object",
  properties: {
    format: {
      type: "string",
      enum: [
        "code128",
        "code128A",
        "code128B",
        "code128C",
        "EAN2",
        "EAN5",
        "EAN8",
        "EAN13",
        "UPC",
        "CODE39",
        "ITF14",
        "MSI",
        "MSI10",
        "MSI11",
        "MSI1010",
        "MSI1110",
        "pharmacode",
        "codabar",
      ],
    },
    width: { type: "number" },
    height: { type: "number" },
    fontSize: { type: "number" },
    margin: { type: "number" },
    background: { type: "string" },
    displayValue: { type: "boolean" },
    disabled: { type: "boolean" },
  },
  additionalProperties: false,
};

const imageOptionsSchema = {
  type: "object",
  properties: {
    maxWidth: { type: "number" },
    maxHeight: { type: "number" },
    width: { type: "number" },
    height: { type: "number" },
    backgroundRemoval: { type: "boolean" },
    backgroundRemovalThreshold: { type: "number" },
    disabled: { type: "boolean" },
  },
  additionalProperties: false,
};

const backgroundOptionsSchema = {
  type: "object",
  properties: {
    disabled: { type: "boolean" },
  },
  additionalProperties: false,
};

const itemSchema = {
  type: "object",
  properties: {
    hidden: { type: "boolean" },
    name: { type: "string" },
    position: positionSchema,
    value: { type: "string" },
    placeholder: { type: "string" },
    group: { type: "string" },
    type: {const: "background"},
}  
  required: ["name", "type", "position", "options"],

  additionalProperties: false,
};

const schema = {
  type: "object",
  properties: {
    name: { type: "string" },
    canvasSize: {
      type: "object",
      properties: {
        width: { type: "number" },
        height: { type: "number" },
      },
    },
    inputs: {
      type: "array",
      items: itemSchema
    },
  },
  required: ["name", "canvasSize", "inputs"],
  additionalProperties: false,
};

I think i need something like this but not sure how to add it to the schema:

oneOf: [
    {
        properties: {
            type: {const: "text"},
            options: textOptionsSchema
        }
    },
    {
        properties: {
            type: {const: "background"},
            options: backgroundOptionsSchema
        }
    }

],

Or if then thing but i couldn't get either to work

0 Answers0