Check json vs schema in javascript

Question:

Is there a simple or native javascript way to test a JSON script for a JSON schema?

I found many libraries on Github, but it does not have a native / simple solution. Does EcmaScript have no specification for this? and none of the browsers (or nodejs) has the ability to check JSON from the start?

Question context:

I have a very complicated circuit that I developed. It should work along with a script that requires JSON data to be passed to it in order to fit the circuit.

+4
source share
1 answer

Simply no.

- JSON Schema, -, 2013 . Internet Drafts - . . , -, , , , .

:

{
  "$schema": "http://json-schema.org/schema#",
  "title": "Product",
  "type": "object",
  "required": ["id", "name", "price"],
  "properties": {
    "id": {
      "type": "number",
      "description": "Product identifier"
    },
    "name": {
      "type": "string",
      "description": "Name of the product"
    },
    "price": {
      "type": "number",
      "minimum": 0
    },
    "tags": {
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    "stock": {
      "type": "object",
      "properties": {
        "warehouse": {
          "type": "number"
        },
        "retail": {
          "type": "number"
        }
      }
    }
  }
}

JSON:

{
  "id": 1,
  "name": "Foo",
  "price": 123,
  "tags": [
    "Bar",
    "Eek"
  ],
  "stock": {
    "warehouse": 300,
    "retail": 20
  }
}

EDIT. ( ) , . . JSON - ajv, .

+2

Source: https://habr.com/ru/post/1651175/


All Articles