{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "https://gb4.7mfe/metaform.schema.json",
  "title": "IMetaForm",
  "description": "Structural schema for gbmetaform .meta.json files — mirrors libs/common/src/lib/gbformgroup/metaform.interfaces.ts. Referential-integrity checks (do trigger/target/dependsOn fieldIds actually exist, are api codes real dot-codes, does PicklistJSON exist in allpicklist.json) are NOT expressible in a structural JSON Schema and are enforced instead by the companion lint script, metaform-lint.ts, in this same directory.",
  "type": "object",
  "required": ["formId", "entity", "title", "pages", "api"],
  "additionalProperties": true,
  "properties": {
    "formId": { "type": "string", "minLength": 1 },
    "entity": { "type": "string", "minLength": 1 },
    "title": { "type": "string", "minLength": 1 },
    "idField": { "type": "string" },
    "capabilities": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "attachment": { "type": "boolean" },
        "print": { "type": "boolean" },
        "draft": { "type": "boolean" },
        "copyFrom": { "type": "boolean" },
        "versioning": { "type": "boolean" },
        "auditLog": { "type": "boolean" },
        "workflow": {
          "type": "object",
          "required": ["acceptApi", "rejectApi"],
          "properties": {
            "acceptApi": { "type": "string" },
            "rejectApi": { "type": "string" }
          }
        }
      }
    },
    "codeDefine": {
      "type": "object",
      "required": ["selectorFieldId", "selectorPicklistJSON", "generationTypeFieldId", "codeFieldId", "keyFieldsApi"],
      "properties": {
        "selectorFieldId": { "type": "string" },
        "selectorPicklistJSON": { "type": "string" },
        "generationTypeFieldId": { "type": "string" },
        "codeFieldId": { "type": "string" },
        "keyFieldsApi": { "type": "string" }
      }
    },
    "pages": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/definitions/page" }
    },
    "quickActions": {
      "type": "array",
      "items": { "$ref": "#/definitions/quickAction" }
    },
    "rules": {
      "type": "array",
      "items": { "$ref": "#/definitions/rule" }
    },
    "serverValidations": {
      "type": "array",
      "items": { "$ref": "#/definitions/serverValidation" }
    },
    "api": {
      "type": "object",
      "required": ["save", "update", "delete"],
      "properties": {
        "save": { "type": "string", "minLength": 1 },
        "update": { "type": "string", "minLength": 1 },
        "delete": { "type": "string", "minLength": 1 },
        "get": { "type": "string" }
      }
    }
  },
  "definitions": {
    "page": {
      "type": "object",
      "required": ["pageId", "pageName"],
      "properties": {
        "pageId": { "type": "string", "minLength": 1 },
        "pageName": { "type": "string", "minLength": 1 },
        "isDynamic": { "type": "boolean" },
        "sections": {
          "type": "array",
          "items": { "$ref": "#/definitions/section" }
        }
      }
    },
    "section": {
      "type": "object",
      "required": ["sectionId"],
      "properties": {
        "sectionId": { "type": "string", "minLength": 1 },
        "sectionName": { "type": "string" },
        "title": { "type": "string" },
        "layout": {
          "type": "object",
          "properties": { "columns": { "type": "number" } }
        },
        "fields": {
          "type": "array",
          "items": { "$ref": "#/definitions/field" }
        },
        "grid": { "$ref": "#/definitions/grid" }
      }
    },
    "field": {
      "type": "object",
      "description": "fieldId or Name (legacy formjson key) must be present — enforced by the lint script, not expressible cleanly as a JSON Schema oneOf without breaking additionalProperties.",
      "properties": {
        "fieldId": { "type": "string" },
        "Name": { "type": "string" },
        "label": { "type": "string" },
        "Label": { "type": "string" },
        "type": { "type": "string" },
        "Type": { "type": "string" },
        "FieldType": { "type": "string" },
        "Required": { "type": "boolean" },
        "mandatory": { "type": "boolean" },
        "ValidRegEx": { "type": "string" },
        "pattern": { "type": "string" },
        "RequireBasedOnFieldName": { "type": "string" },
        "RequireBasedOnFieldValue": { "type": "array" },
        "Width": { "type": "number" },
        "LabelWidth": { "type": "number" },
        "layout": {
          "type": "object",
          "properties": { "colSpan": { "type": "number", "minimum": 1, "maximum": 12 } }
        },
        "ColSpan": { "type": "number" },
        "DefaultValue": {},
        "MaxLength": { "type": "number" },
        "PlaceHolder": { "type": "string" },
        "placeholder": { "type": "string" },
        "PostData": { "type": "boolean" },
        "ReadOnly": { "type": "boolean" },
        "readonly": { "type": "boolean" },
        "IsVisible": { "type": "boolean" },
        "isKeyField": { "type": "boolean" },
        "AddNew": { "type": "boolean" },
        "IsFirstField": { "type": "boolean" },
        "PicklistJSON": { "type": "string" },
        "PicklistTitle": { "type": "string" },
        "PicklistSubTitle": { "type": "string" },
        "PicklistCriteria": { "type": "string" },
        "LinkId": { "type": "string" },
        "LinkSubTitle": { "type": "string" },
        "LinkValue": { "type": "string" },
        "Multiple": { "type": "boolean" },
        "PickLists": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["key", "value"],
            "properties": { "key": { "type": ["number", "string"] }, "value": { "type": "string" } }
          }
        },
        "lookup": {
          "type": "object",
          "required": ["valueField", "displayField"],
          "properties": {
            "api": { "type": "string" },
            "valueField": { "type": "string" },
            "displayField": { "type": "string" },
            "dependsOn": { "type": "array", "items": { "type": "string" } },
            "options": { "type": "array", "items": { "type": "object" } }
          }
        },
        "Grid": { "type": "array", "items": { "$ref": "#/definitions/grid" } },
        "DependendFieldName": { "type": "string" },
        "DependendFieldValue": { "type": "string" },
        "linkedView": {
          "type": "object",
          "required": ["type"],
          "properties": {
            "type": { "enum": ["form", "report"] },
            "entity": { "type": "string" },
            "metaReportId": { "type": "number" },
            "paramMapping": { "type": "object" }
          }
        }
      }
    },
    "grid": {
      "type": "object",
      "required": ["fieldId"],
      "properties": {
        "fieldId": { "type": "string", "minLength": 1 },
        "entity": { "type": "string" },
        "columns": { "type": "array", "items": { "$ref": "#/definitions/field" } },
        "allowAdd": { "type": "boolean" },
        "allowDelete": { "type": "boolean" },
        "IsAddrowRequired": { "type": "boolean" },
        "IsCheckBoxRequired": { "type": "boolean" },
        "isStore": { "type": "boolean" },
        "CheckBoxField": { "type": "string" },
        "SLNOField": { "type": "string" },
        "DeleteUrl": { "type": "string" },
        "DeleteParameterFieldName": { "type": "string" }
      }
    },
    "quickAction": {
      "type": "object",
      "required": ["actionId", "label", "fields", "api"],
      "properties": {
        "actionId": { "type": "string", "minLength": 1 },
        "label": { "type": "string", "minLength": 1 },
        "fields": { "type": "array", "items": { "$ref": "#/definitions/field" } },
        "api": { "type": "string", "minLength": 1 },
        "visibleWhen": { "type": "string" }
      }
    },
    "rule": {
      "type": "object",
      "required": ["id", "scope", "trigger", "type"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "scope": { "enum": ["developer", "implementation"] },
        "trigger": { "type": "array", "items": { "type": "string" }, "minItems": 1 },
        "type": { "enum": ["calculate", "validate", "visibility", "enable"] }
      },
      "allOf": [
        {
          "if": { "properties": { "type": { "const": "calculate" } } },
          "then": { "required": ["formula", "target"] }
        },
        {
          "if": { "properties": { "type": { "const": "validate" } } },
          "then": { "required": ["condition", "onFail", "errorMessage"] }
        },
        {
          "if": { "properties": { "type": { "enum": ["visibility", "enable"] } } },
          "then": { "required": ["condition", "targets", "action"] }
        }
      ]
    },
    "serverValidation": {
      "type": "object",
      "required": ["id", "trigger", "fields", "api", "params", "severity"],
      "properties": {
        "id": { "type": "string", "minLength": 1 },
        "trigger": { "type": "array", "items": { "type": "string" } },
        "fields": { "type": "array", "items": { "type": "string" } },
        "api": { "type": "string", "minLength": 1 },
        "params": { "type": "object" },
        "updateFields": { "type": "array", "items": { "type": "string" } },
        "errorField": { "type": "string" },
        "severity": { "enum": ["error", "warning"] },
        "condition": { "type": "string" }
      }
    }
  }
}
