18. Chai Json Schema

🤩 Kolay Yöntem

Schema Generator ile kolaylıkla json'dan schema elde edebilirsiniz.

JSON data type

type anahtar sözcüğü, verinin belirli bir türde (veya bazı türlerde) olmasını gerektirir.

Type şunlar olabilir : number, integer, string, boolean, array, object ve null

Örnek

  1. schema: {type: "number"} Doğru data: 1, 1.5 Yanlış data: "osman", "1", [], {}, null, false

  2. schema: {type: "integer"} Doğru data: 1, 2 Yanlış data: "abc", "1", 1.5, [], {}, null, true

  3. schema: {type: ["number", "string"]} Doğru data: 1, 1.5, "abc", "1" Yanlış Data:[], {}, null, true

nullable

// 1.seçenek
{
  "type": "string",
  "nullable": true
}

// 2.seçenek
{
  "type": ["string", "null"]
}

maximum / minimum and exclusiveMaximum / exclusiveMinimum

1. schema: {type: "number", maximum: 5}
valid: 4, 5
invalid: 6, 7


2. schema: {type: "number", minimum: 5}
valid: 5, 6
invalid: 4, 4.5


3. schema: {type: "number", exclusiveMinimum: 5}
valid: 6, 7
invalid: 4.5, 5


4. schema: {type: "number", multipleOf: 5}
valid: 5, 10
invalid: 1, 4


5. schema: {type: "number", multipleOf: 2.5}
valid: 2.5, 5, 7.5
invalid: 1, 4

String type için

1. schema: {type: "string", maxLength: 5}
valid: "abc", "abcde"
invalid: "abcdef"


2. schema: {type: "string", minLength: 2}
valid: "ab", "🍊🍊🍊"
invalid: "a", "🍊"

3. schema: {type: "string", pattern: "[abc]+"}
valid: "a", "abcd", "cde"
invalid: "def", ""

4. schema: {type: "string", format: "ipv4"}
valid: "192.168.0.1"
invalid: "abc"

Array type için

1. schema: {type: "array", maxItems: 3}
valid: [], [1], ["1", 2, "3"]
invalid: [1, 2, 3, 4]

2. schema: {type: "array", uniqueItems: true}
valid: [], [1], ["1", 2, "3"]
invalid: [1, 2, 1], [{a: 1, b: 2}, {b: 2, a: 1}]

3. schema: {type: "array", items: {type: "integer"}}
valid: [1,2,3], []
invalid: [1,"abc"]
4. schema: {
  type: "array",
  items: [{type: "integer"}, {type: "string"}]
}
valid: [1], [1, "abc"], [1, "abc", 2], []
invalid: ["abc", 1], ["abc"]

5. schema: {
  type: "array",
  prefixItems: [{type: "integer"}, {type: "integer"}],
  items: {type: "string"}
}
valid: [], [1, 2], [1, 2, "abc"]
invalid: ["abc"], [1, 2, 3]

6. shema: {
  type: "array",
  items: [{type: "integer"}, {type: "integer"}],
  minItems: 2
  additionalItems: false
}
valid: [1, 2]
invalid: [], [1], [1, 2, 3], [1, "abc"] (any wrong number of items or wrong type)

7.schema: {type: "array", contains: {type: "integer"}}
valid: [1], [1, "foo"], any array with at least one integer
invalid: [], ["foo", "bar"], any array without integers

#

Object type için

1. schema: {type: "object", maxProperties: 2 }
valid: {}, {a: 1}, {a: "1", b: 2}
invalid: {a: 1, b: 2, c: 3}

2.schema: {type: "object", required: ["a", "b"]}
valid: {a: 1, b: 2}, {a: 1, b: 2, c: 3}
invalid: {}, {a: 1}, {c: 3, d: 4}

3. schema: {
  type: "object",
  properties: {
    foo: {type: "string"},
    bar: {
      type: "number",
      minimum: 2
    }
  }
}
valid: {}, {foo: "a"}, {foo: "a", bar: 2}
invalid: {foo: 1}, {foo: "a", bar: 1}

Çok önemli bir field: anyOf

4. schema: {
  type: "object",
  properties: {
    foo: {type: "number"}
  },
  additionalProperties: false,
  anyOf: [
    {
      properties: {
        bar: {type: "number"}
      }
    },
    {
      properties: {
        baz: {type: "number"}
      }
    }
  ]
}
valid: {}, {foo: 1}
invalid: {bar: 2}, {baz: 3}, {foo: 1, bar: 2}, etc.

oneOf

5.schema: {
  type: "number",
  oneOf: [{maximum: 3}, {type: "integer"}]
}
valid: 1.5, 2.5, 4, 5
invalid: 2, 3, 4.5, 5.5

if / then / else

1. schema: {
  type: "object",
  if: {properties: {foo: {minimum: 10}}},
  then: {required: ["bar"]},
  else: {required: ["baz"]}
}

valid:

{foo: 10, bar: true }
{}
{foo: 1, baz: true }
invalid:

{foo: 10} (bar is required)
{foo: 10, baz: true } (bar is required)
{foo: 1} (baz is required)

Last updated