Skip to content

Commit

Permalink
add json samples
Browse files Browse the repository at this point in the history
  • Loading branch information
tymondesigns committed Jan 13, 2025
1 parent d142b05 commit 460d26f
Showing 1 changed file with 158 additions and 2 deletions.
160 changes: 158 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,24 @@ SchemaFactory::string('name')
->writeOnly();
```

<details>
<summary>View JSON Schema</summary>

```json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": ["string", "null"],
"title": "name",
"minLength": 2,
"maxLength": 100,
"pattern": "^[A-Za-z]+$",
"format": "email",
"readOnly": true,
"writeOnly": true
}
```
</details>

### Number Schema

```php
Expand All @@ -82,6 +100,23 @@ SchemaFactory::number('price')
->nullable();
```

<details>
<summary>View JSON Schema</summary>

```json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": ["number", "null"],
"title": "price",
"minimum": 0,
"maximum": 1000,
"exclusiveMinimum": 0,
"exclusiveMaximum": 1000,
"multipleOf": 0.01
}
```
</details>

### Integer Schema

```php
Expand All @@ -94,6 +129,23 @@ SchemaFactory::integer('age')
->nullable();
```

<details>
<summary>View JSON Schema</summary>

```json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": ["integer", "null"],
"title": "age",
"minimum": 0,
"maximum": 120,
"exclusiveMinimum": 0,
"exclusiveMaximum": 120,
"multipleOf": 1
}
```
</details>

### Boolean Schema

```php
Expand All @@ -103,13 +155,40 @@ SchemaFactory::boolean('active')
->readOnly();
```

<details>
<summary>View JSON Schema</summary>

```json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": ["boolean", "null"],
"title": "active",
"default": true,
"readOnly": true
}
```
</details>

### Null Schema

```php
SchemaFactory::null('deleted_at')
->readOnly();
```

<details>
<summary>View JSON Schema</summary>

```json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "null",
"title": "deleted_at",
"readOnly": true
}
```
</details>

### Array Schema

```php
Expand All @@ -134,6 +213,48 @@ SchemaFactory::array('coordinates')
->maxItems(2);
```

<details>
<summary>View JSON Schema</summary>

```json
{
// Simple array of strings
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"title": "tags",
"items": {
"type": "string"
},
"minItems": 1,
"maxItems": 10,
"uniqueItems": true
}

{
// Tuple validation
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "array",
"title": "coordinates",
"prefixItems": [
{
"type": "number",
"title": "latitude",
"minimum": -90,
"maximum": 90
},
{
"type": "number",
"title": "longitude",
"minimum": -180,
"maximum": 180
}
],
"minItems": 2,
"maxItems": 2
}
```
</details>

### Object Schema

```php
Expand All @@ -150,6 +271,42 @@ SchemaFactory::object('user')
->additionalProperties(false);
```

<details>
<summary>View JSON Schema</summary>

```json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "user",
"properties": {
"name": {
"type": "string",
"title": "name"
},
"email": {
"type": "string",
"title": "email"
},
"settings": {
"type": "object",
"title": "settings",
"properties": {
"theme": {
"type": "string",
"title": "theme"
}
}
}
},
"required": ["name", "email"],
"minProperties": 1,
"maxProperties": 10,
"additionalProperties": false
}
```
</details>

## Validation

The library throws a `SchemaException` when validation fails:
Expand Down Expand Up @@ -188,8 +345,7 @@ $jsonSchemaArray = $schema->toArray();

// Convert to JSON string
$jsonSchemaString = $schema->toJson();
// or with pretty printing
$jsonSchemaString = $schema->toJson(JSON_PRETTY_PRINT);
$jsonSchemaString = $schema->toJson(JSON_PRETTY_PRINT); // with pretty printing
```

This will output a valid JSON Schema that can be used with any JSON Schema validator.
Expand Down

0 comments on commit 460d26f

Please sign in to comment.