> For the complete documentation index, see [llms.txt](https://furkans-organization-1.gitbook.io/cypress-notlari/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://furkans-organization-1.gitbook.io/cypress-notlari/18.-chai-json-schema.md).

# 18. Chai Json Schema

## 🤩 Kolay Yöntem&#x20;

&#x20;[Schema Generator](https://fport.github.io/schema-generator/) ile kolaylıkla json'dan schema elde edebilirsiniz.

<figure><img src="/files/LBcodBBbxT1PumLcUaqf" alt=""><figcaption><p><a href="https://fport.github.io/schema-generator/">https://fport.github.io/schema-generator/</a></p></figcaption></figure>

## JSON data <mark style="color:orange;">type</mark> <a href="#json-data-type" id="json-data-type"></a>

**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

### <mark style="color:orange;">Örnek</mark>

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`

#### <mark style="color:orange;">`nullable`</mark> <a href="#nullable" id="nullable"></a>

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

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

#### <mark style="color:orange;">`maximum`</mark> <mark style="color:orange;"></mark><mark style="color:orange;">/</mark> <mark style="color:orange;"></mark><mark style="color:orange;">`minimum`</mark> <mark style="color:orange;"></mark><mark style="color:orange;">and</mark> <mark style="color:orange;"></mark><mark style="color:orange;">`exclusiveMaximum`</mark> <mark style="color:orange;"></mark><mark style="color:orange;">/</mark> <mark style="color:orange;"></mark><mark style="color:orange;">`exclusiveMinimum`</mark> <a href="#maximum-minimum-and-exclusivemaximum-exclusiveminimum" id="maximum-minimum-and-exclusivemaximum-exclusiveminimum"></a>

```
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

```

## &#x20;<mark style="color:orange;">**String**</mark>**&#x20;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"

```

## <mark style="color:orange;">Array</mark> type için <a href="#maxlength-minlength" id="maxlength-minlength"></a>

```
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

#
```

## <mark style="color:orange;">Object</mark> type için <a href="#maxitems-minitems" id="maxitems-minitems"></a>

```
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:&#x20;**<mark style="color:red;">**anyOf**</mark>&#x20;

```
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.
```

<mark style="color:yellow;">**`oneOf`**</mark>

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

## <mark style="color:orange;">if / then / else</mark>

```
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)
```
