Как проверить сложную схему с помощью мета-схемы? - PullRequest
0 голосов
/ 29 мая 2019

Я пытаюсь проверить некоторые схемы, которые у меня уже есть, для мета-схемы, используя библиотеку Ajv.Я получаю результат, что схемы действительны, даже если я изменю их на недопустимое определение.

Это одна схема:

{
  "id": "TEST_SCHEMA",
  "name": "TEST_SCHEMA",
  "type": "object",
  "properties": {
    "isEnabled": {
      "type": "boolean",
      "name": "isEnabled",
      "default": false
    },
    "options": {
      "type": "array",
      "name": "options",
      "items": {
        "type": "object",
        "properties": {
          "content": {
            "type": "string",
            "name": "content",
            "default": ""
          },
          "isChecked": {
            "type": "boolean",
            "name": "isChecked",
            "default": true
          },
          "entities": {
            "type": "array",
            "name": "entities",
            "uniqueItems": true,
            "items": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string",
                  "enum": [
                    "club",
                    "businessPartners"
                  ]
                }
              }
            }
          },
          "channels": {
            "type": "array",
            "name": "channels",
            "uniqueItems": true,
            "items": {
              "type": "object",
              "properties": {
                "name": {
                  "type": "string",
                  "enum": [
                    "email",
                    "phone",
                    "sms",
                    "post"
                  ]
                }
              }
            }
          }
        }
      }
    }
  }
}

Это моя мета-схема:

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "http://www.example.com/meta-schema.json#",
  "title": "Core schema meta-schema",
  "definitions": {
    "schemaArray": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#" }
    },
    "schemaObject": {
      "type": "object",
      "minProperties": 1,
      "patternProperties": {
        "^([A-Za-z0-9_$]){2,}$": {"$ref": "#"}
      },
      "additionalProperties": fanlse
    },
    "simpleTypes": {
      "enum": [
        "array",
        "boolean",
        "integer",
        "null",
        "number",
        "object",
        "string",
        "function"
      ]
    },
    "stringArray": {
      "type": "array",
      "items": { "type": "string" },
      "uniqueItems": true,
      "default": []
    }
  },
  "type": ["object", "boolean"],
  "properties": {
    "id": {
      "type": "string",
      "format": "uri-reference"
    },
    "$ref": {
      "type": "string",
      "format": "uri-reference"
    },
    "title": {
      "type": "string"
    },
    "description": {
      "type": "string"
    },
    "default": true,
    "readOnly": {
      "type": "boolean",
      "default": false
    },
    "pattern": {
      "type": "string",
      "format": "regex"
    },
    "items": {
      "$ref": "#",
      "default": true
    },
    "uniqueItems": {
      "type": "boolean",
      "default": false
    },
    "required": { "$ref": "#/definitions/stringArray" },
    "additionalProperties": { "$ref": "#" },
    "definitions": {
      "type": "object",
      "additionalProperties": { "$ref": "#" },
      "default": {}
    },
    "properties": {
      "$ref": "#",
      "default": true
    },
    "dependencies": {
      "type": "object",
      "additionalProperties": {
        "anyOf": [
          { "$ref": "#" },
          { "$ref": "#/definitions/stringArray" }
        ]
      }
    },
    "propertyNames": { "$ref": "#" },
    "enum": {
      "type": "array",
      "items": true,
      "minItems": 1,
      "uniqueItems": true
    },
    "type": {
      "anyOf": [
        { "$ref": "#/definitions/simpleTypes" },
        {
          "type": "array",
          "items": { "$ref": "#/definitions/simpleTypes" },
          "minItems": 1,
          "uniqueItems": true
        }
      ]
    }
  },
  "default": true
}

Как я использую Ajv:

  const ajv = new Ajv({
    allErrors: true,
    schemaId: `auto`,
    extendRefs: `fail`,
    schemas: refs,
    meta: META_SCHEMA
  })

  try {
    ajv.validateSchema(schema)
  } catch (error) {
    console.error(`Schema Validation Error:`, file, error)
    console.table(ajv.errors, [`keyword`,`params`,`message`])
  }

Я ожидаю сбоя схем, когда ониневерное определение.

Моя мета-схема неверна или я неправильно использую Ajv?

...