Skip to content

Commit

Permalink
test: Additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tymondesigns committed Jan 18, 2025
1 parent 28d0eff commit ea4d026
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Types/ArraySchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function contains(Schema $schema): static
public function minContains(int $min): static
{
if ($min < 0) {
throw new SchemaException('minContains must be non-negative');
throw new SchemaException('minContains must be greater than or equal to 0');
}

if ($this->maxContains !== null && $min > $this->maxContains) {
Expand All @@ -63,7 +63,7 @@ public function minContains(int $min): static
public function maxContains(int $max): static
{
if ($max < 0) {
throw new SchemaException('maxContains must be non-negative');
throw new SchemaException('maxContains must be greater than or equal to 0');
}

if ($this->minContains !== null && $max < $this->minContains) {
Expand Down
6 changes: 6 additions & 0 deletions tests/ArchitectureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@

namespace Cortex\JsonSchema\Tests;

use Throwable;

arch()->preset()->php();
arch()->preset()->security();

arch()->expect('Cortex\JsonSchema\Contracts')->toBeInterfaces();
arch()->expect('Cortex\JsonSchema\Enums')->toBeEnums();
arch()->expect('Cortex\JsonSchema\Exceptions')->toExtend(Throwable::class);
2 changes: 2 additions & 0 deletions tests/Unit/SchemaFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,6 @@
'fooArray',
],
]);

expect($schema->toJson())->toBe(json_encode($schema->toArray()));
});
12 changes: 12 additions & 0 deletions tests/Unit/Targets/ArraySchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,3 +123,15 @@
'At least one array item must match schema',
);
});

it('throws an exception if the minContains is less than 0', function (): void {
Schema::array('numbers')
->description('List of numbers')
->minContains(-1);
})->throws(SchemaException::class, 'minContains must be greater than or equal to 0');

it('throws an exception if the maxContains is less than 0', function (): void {
Schema::array('numbers')
->description('List of numbers')
->maxContains(-1);
})->throws(SchemaException::class, 'maxContains must be greater than or equal to 0');
6 changes: 6 additions & 0 deletions tests/Unit/Targets/IntegerSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,9 @@
'The data (string) must match the type: integer, null',
);
});

it('throws an exception if the multipleOf is less than 0', function (): void {
Schema::integer('age')
->description('User age')
->multipleOf(-1);
})->throws(SchemaException::class, 'multipleOf must be greater than 0');
22 changes: 22 additions & 0 deletions tests/Unit/Targets/ObjectSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,25 @@
'key2' => 'value2',
]))->not->toThrow(SchemaException::class);
});

it('can specify a propertyNames schema', function (): void {
$schema = Schema::object('user')
->properties(
Schema::string('name')->required(),
)
// ->additionalProperties(true)
->propertyNames(Schema::string()->pattern('^[a-zA-Z]+$'));

$schemaArray = $schema->toArray();

expect($schemaArray)->toHaveKey('propertyNames.pattern', '^[a-zA-Z]+$');

// Validation tests
expect(fn() => $schema->validate([
'name' => 'John Doe',
]))->not->toThrow(SchemaException::class);

expect(fn() => $schema->validate([
'name' => 123, // invalid property name pattern
]))->toThrow(SchemaException::class, 'The properties must match schema: name');
});
10 changes: 10 additions & 0 deletions tests/Unit/Targets/StringSchemaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@
);

expect(fn() => $schema->validate('[email protected]'))->not->toThrow(SchemaException::class);

$schema = Schema::string('email')
->description('User email address')
->format('custom');

expect($schema->toArray())->toHaveKey('format', 'custom');
});

it('can create a nullable string schema', function (): void {
Expand All @@ -122,10 +128,14 @@
expect(fn() => $schema->validate(null))->not->toThrow(SchemaException::class);
expect(fn() => $schema->validate('John'))->not->toThrow(SchemaException::class);

expect($schema->isValid(null))->toBeTrue();
expect($schema->isValid('John'))->toBeTrue();

expect(fn() => $schema->validate(123))->toThrow(
SchemaException::class,
'The data (integer) must match the type: string, null',
);
expect($schema->isValid(123))->toBeFalse();
});

it('can create a read-only string schema', function (): void {
Expand Down

0 comments on commit ea4d026

Please sign in to comment.