Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tymondesigns committed Jan 13, 2025
1 parent ea7e695 commit 7a03612
Show file tree
Hide file tree
Showing 12 changed files with 1,265 additions and 43 deletions.
189 changes: 183 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,204 @@ composer require cortex/json-schema
```php
use Cortex\JsonSchema\SchemaFactory;

// Create a basic user schema
$schema = SchemaFactory::object('user')
->description('User schema')
->properties(
SchemaFactory::string('name'),
SchemaFactory::string('email'),
);
->properties([
'name' => SchemaFactory::string('name')
->minLength(2)
->maxLength(100),
'email' => SchemaFactory::string('email')
->format('email'),
'age' => SchemaFactory::integer('age')
->minimum(0)
->maximum(120),
'active' => SchemaFactory::boolean('active')
->default(true),
'settings' => SchemaFactory::object('settings')
->additionalProperties(false)
->properties([
'theme' => SchemaFactory::string('theme')
->enum(['light', 'dark']),
'notifications' => SchemaFactory::boolean('notifications')
])
])
->required(['name', 'email']);

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

// Validate data
$schema->validate([
'name' => 'John Doe',
'email' => '[email protected]',
'age' => 30,
'active' => true,
'settings' => [
'theme' => 'dark',
'notifications' => true
]
]);
```

## Available Schema Types

### String Schema

```php
SchemaFactory::string('name')
->minLength(2)
->maxLength(100)
->pattern('^[A-Za-z]+$')
->format('email') // Available formats: date-time, email, hostname, ipv4, ipv6, uri
->nullable()
->readOnly()
->writeOnly();
```

### Number Schema

```php
SchemaFactory::number('price')
->minimum(0)
->maximum(1000)
->exclusiveMinimum(0)
->exclusiveMaximum(1000)
->multipleOf(0.01)
->nullable();
```

### Integer Schema

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

### Boolean Schema

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

### Null Schema

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

### Array Schema

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

// Tuple validation (fixed array format)
SchemaFactory::array('coordinates')
->prefixItems([
SchemaFactory::number('latitude')
->minimum(-90)
->maximum(90),
SchemaFactory::number('longitude')
->minimum(-180)
->maximum(180)
])
->minItems(2)
->maxItems(2);
```

### Object Schema

```php
SchemaFactory::object('user')
->properties([
'name' => SchemaFactory::string('name'),
'email' => SchemaFactory::string('email'),
'settings' => SchemaFactory::object('settings')
->properties([
'theme' => SchemaFactory::string('theme')
])
])
->required(['name', 'email'])
->minProperties(1)
->maxProperties(10)
->additionalProperties(false);
```

## Validation

The library throws a `SchemaException` when validation fails:

```php
use Cortex\JsonSchema\Exceptions\SchemaException;

try {
$schema->validate($data);
} catch (SchemaException $e) {
echo $e->getMessage(); // "The data must match the 'email' format"
}
```

## Common Schema Properties

All schema types support these common properties:

```php
$schema
->title('Schema Title')
->description('Schema description')
->default('default value')
->examples(['example1', 'example2'])
->readOnly()
->writeOnly();
```

## Converting to JSON Schema

You can convert any schema to a JSON Schema array or JSON string:

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

// Convert to JSON string
$jsonSchemaString = $schema->toJson();
// or 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.

Example JSON output:
```json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "user",
"description": "User schema",
"required": ["name", "email"],
"properties": {
"name": {
"type": "string"
"type": "string",
"minLength": 2,
"maxLength": 100
},
"email": {
"type": "string"
"type": "string",
"format": "email"
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/Types/AbstractSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,12 @@ public function nullable(): static
}

if (is_array($this->type)) {
$this->type = [...$this->type, SchemaType::Null];
$this->type[] = SchemaType::Null;
} else {
$this->type = SchemaType::Null;
$this->type = [
$this->type,
SchemaType::Null,
];
}

return $this;
Expand Down
106 changes: 103 additions & 3 deletions src/Types/IntegerSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,113 @@

namespace Cortex\JsonSchema\Types;

use Override;
use Cortex\JsonSchema\Enums\SchemaType;
use Cortex\JsonSchema\Exceptions\SchemaException;

class IntegerSchema extends NumberSchema
class IntegerSchema extends AbstractSchema
{
protected ?int $minimum = null;

protected ?int $maximum = null;

protected ?int $exclusiveMinimum = null;

protected ?int $exclusiveMaximum = null;

protected ?int $multipleOf = null;

public function __construct(?string $title = null)
{
parent::__construct($title);
$this->type = SchemaType::Integer;
parent::__construct(SchemaType::Integer, $title);
}

/**
* Set the minimum value (inclusive).
*/
public function minimum(int $value): static
{
$this->minimum = $value;

return $this;
}

/**
* Set the maximum value (inclusive).
*/
public function maximum(int $value): static
{
$this->maximum = $value;

return $this;
}

/**
* Set the exclusive minimum value.
*/
public function exclusiveMinimum(int $value): static
{
$this->exclusiveMinimum = $value;

return $this;
}

/**
* Set the exclusive maximum value.
*/
public function exclusiveMaximum(int $value): static
{
$this->exclusiveMaximum = $value;

return $this;
}

/**
* Set the multipleOf value.
*
* @throws \Cortex\JsonSchema\Exceptions\SchemaException
*/
public function multipleOf(int $value): static
{
if ($value <= 0) {
throw new SchemaException('multipleOf must be greater than 0');
}

$this->multipleOf = $value;

return $this;
}

/**
* Convert to array.
*
* @return array<string, mixed>
*/
#[Override]
public function toArray(bool $includeSchemaRef = true, bool $includeTitle = true): array
{
$schema = parent::toArray($includeSchemaRef, $includeTitle);

if ($this->minimum !== null) {
$schema['minimum'] = $this->minimum;
}

if ($this->maximum !== null) {
$schema['maximum'] = $this->maximum;
}

if ($this->exclusiveMinimum !== null) {
$schema['exclusiveMinimum'] = $this->exclusiveMinimum;
}

if ($this->exclusiveMaximum !== null) {
$schema['exclusiveMaximum'] = $this->exclusiveMaximum;
}

if ($this->multipleOf !== null) {
$schema['multipleOf'] = $this->multipleOf;
}

return $schema;
}
}
32 changes: 0 additions & 32 deletions tests/Unit/ObjectSchemaTest.php

This file was deleted.

Loading

0 comments on commit 7a03612

Please sign in to comment.