Skip to content

Commit

Permalink
docs: Update Readme
Browse files Browse the repository at this point in the history
  • Loading branch information
tymondesigns committed Jan 14, 2025
1 parent ad08846 commit cb78a5e
Showing 1 changed file with 104 additions and 42 deletions.
146 changes: 104 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# json-schema
# Fluently build and validate JSON Schemas

A PHP library for fluently building and validating JSON Schemas.
[![Latest Version](https://img.shields.io/packagist/v/cortexphp/json-schema.svg?style=flat-square&logo=composer)](https://packagist.org/packages/cortexphp/json-schema)
![GitHub License](https://img.shields.io/github/license/cortexphp/json-schema?style=flat-square&logo=github)

Currently only supports https://json-schema.org/draft-07.

## Installation

Expand Down Expand Up @@ -37,7 +40,9 @@ $schema = SchemaFactory::object('user')
SchemaFactory::boolean('notifications')
]),
);
```

```php
// Convert to array
$schema->toArray();

Expand Down Expand Up @@ -76,24 +81,30 @@ $schema = SchemaFactory::string('name')
->minLength(2)
->maxLength(100)
->pattern('^[A-Za-z]+$')
->nullable()
->readOnly();
```

```php
$schema->isValid('John Doe'); // true
$schema->isValid('John Doe123'); // false (contains numbers)
$schema->isValid('J'); // false (too short)
```

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

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

</details>


Expand All @@ -103,8 +114,15 @@ use Cortex\JsonSchema\Enums\SchemaFormat;

$schema = SchemaFactory::string('email')
->format(SchemaFormat::Email)
->nullable()
->nullable();
```

```php
$schema->isValid('[email protected]'); // true
$schema->isValid('foo'); // false
$schema->isValid(null); // true
```

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

Expand All @@ -115,19 +133,24 @@ $schema = SchemaFactory::string('email')
"format": "email"
}
```

</details>

---

### Number Schema

```php
SchemaFactory::number('price')
$schema = SchemaFactory::number('price')
->minimum(0)
->maximum(1000)
->exclusiveMinimum(0)
->exclusiveMaximum(1000)
->multipleOf(0.01)
->nullable();
->multipleOf(0.01);
```
```php
$schema->isValid(100); // true
$schema->isValid(1000.01); // false (too high)
$schema->isValid(0.01); // true
$schema->isValid(1.011); // false (not a multiple of 0.01)
```

<details>
Expand All @@ -136,27 +159,30 @@ SchemaFactory::number('price')
```json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": ["number", "null"],
"type": "number",
"title": "price",
"minimum": 0,
"maximum": 1000,
"exclusiveMinimum": 0,
"exclusiveMaximum": 1000,
"multipleOf": 0.01
}
```

</details>

### Integer Schema

```php
SchemaFactory::integer('age')
->minimum(0)
->maximum(120)
$schema = SchemaFactory::integer('age')
->exclusiveMinimum(0)
->exclusiveMaximum(120)
->multipleOf(1)
->nullable();
->exclusiveMaximum(150)
->multipleOf(1);
```

```php
$schema->isValid(18); // true
$schema->isValid(150); // false (too high)
$schema->isValid(0); // false (too low)
$schema->isValid(150.01); // false (not an integer)
```

<details>
Expand All @@ -165,12 +191,10 @@ SchemaFactory::integer('age')
```json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": ["integer", "null"],
"type": "integer",
"title": "age",
"minimum": 0,
"maximum": 120,
"exclusiveMinimum": 0,
"exclusiveMaximum": 120,
"exclusiveMaximum": 150,
"multipleOf": 1
}
```
Expand All @@ -179,19 +203,24 @@ SchemaFactory::integer('age')
### Boolean Schema

```php
SchemaFactory::boolean('active')
$schema = SchemaFactory::boolean('active')
->default(true)
->nullable()
->readOnly();
```

```php
$schema->isValid(true); // true
$schema->isValid(false); // true
$schema->isValid(null); // false
```

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

```json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": ["boolean", "null"],
"type": "boolean",
"title": "active",
"default": true,
"readOnly": true
Expand All @@ -202,8 +231,13 @@ SchemaFactory::boolean('active')
### Null Schema

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

```php
$schema->isValid(null); // true
$schema->isValid(true); // false
$schema->isValid(false); // false
```

<details>
Expand All @@ -213,8 +247,7 @@ SchemaFactory::null('deleted_at')
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "null",
"title": "deleted_at",
"readOnly": true
"title": "deleted_at"
}
```
</details>
Expand All @@ -223,27 +256,33 @@ SchemaFactory::null('deleted_at')

```php
// Simple array of strings
SchemaFactory::array('tags')
$schema = SchemaFactory::array('tags')
->items(SchemaFactory::string())
->minItems(1)
->maxItems(10)
->maxItems(3)
->uniqueItems(true);
```

```php
$schema->isValid(['foo', 'bar']); // true
$schema->isValid(['foo', 'foo']); // false (not unique)
$schema->isValid([]); // false (too few items)
$schema->isValid(['foo', 'bar', 'baz', 'qux']); // false (too many items)
```

<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,
"maxItems": 3,
"uniqueItems": true
}
```
Expand All @@ -252,19 +291,44 @@ SchemaFactory::array('tags')
### Object Schema

```php
SchemaFactory::object('user')
use Cortex\JsonSchema\SchemaFactory;
use Cortex\JsonSchema\Enums\SchemaFormat;

$schema = SchemaFactory::object('user')
->properties(
SchemaFactory::string('name')->required(),
SchemaFactory::string('email')->required(),
SchemaFactory::string('email')->format(SchemaFormat::Email)->required(),
SchemaFactory::object('settings')->properties(
SchemaFactory::string('theme')
SchemaFactory::string('theme')->enum(['light', 'dark'])
),
)
->minProperties(1)
->maxProperties(10)
->additionalProperties(false);
```

```php
$schema->isValid([
'name' => 'John Doe',
'email' => '[email protected]',
]); // true

$schema->isValid([
'name' => 'John Doe',
'email' => '[email protected]',
'settings' => [
'theme' => 'dark',
],
]); // true

$schema->isValid([
'name' => 'John Doe',
'email' => '[email protected]',
'settings' => [
'theme' => 'dark',
],
'foo' => 'bar',
]); // false (additional properties)
```

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

Expand Down Expand Up @@ -294,8 +358,6 @@ SchemaFactory::object('user')
}
},
"required": ["name", "email"],
"minProperties": 1,
"maxProperties": 10,
"additionalProperties": false
}
```
Expand Down Expand Up @@ -340,7 +402,7 @@ $jsonSchemaArray = $schema->toArray();

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

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

0 comments on commit cb78a5e

Please sign in to comment.