Skip to content

Commit ea4d026

Browse files
committed
test: Additional tests
1 parent 28d0eff commit ea4d026

7 files changed

Lines changed: 60 additions & 2 deletions

File tree

src/Types/ArraySchema.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function contains(Schema $schema): static
4343
public function minContains(int $min): static
4444
{
4545
if ($min < 0) {
46-
throw new SchemaException('minContains must be non-negative');
46+
throw new SchemaException('minContains must be greater than or equal to 0');
4747
}
4848

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

6969
if ($this->minContains !== null && $max < $this->minContains) {

tests/ArchitectureTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,11 @@
44

55
namespace Cortex\JsonSchema\Tests;
66

7+
use Throwable;
8+
79
arch()->preset()->php();
810
arch()->preset()->security();
11+
12+
arch()->expect('Cortex\JsonSchema\Contracts')->toBeInterfaces();
13+
arch()->expect('Cortex\JsonSchema\Enums')->toBeEnums();
14+
arch()->expect('Cortex\JsonSchema\Exceptions')->toExtend(Throwable::class);

tests/Unit/SchemaFactoryTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,6 @@
8787
'fooArray',
8888
],
8989
]);
90+
91+
expect($schema->toJson())->toBe(json_encode($schema->toArray()));
9092
});

tests/Unit/Targets/ArraySchemaTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,15 @@
123123
'At least one array item must match schema',
124124
);
125125
});
126+
127+
it('throws an exception if the minContains is less than 0', function (): void {
128+
Schema::array('numbers')
129+
->description('List of numbers')
130+
->minContains(-1);
131+
})->throws(SchemaException::class, 'minContains must be greater than or equal to 0');
132+
133+
it('throws an exception if the maxContains is less than 0', function (): void {
134+
Schema::array('numbers')
135+
->description('List of numbers')
136+
->maxContains(-1);
137+
})->throws(SchemaException::class, 'maxContains must be greater than or equal to 0');

tests/Unit/Targets/IntegerSchemaTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,9 @@
156156
'The data (string) must match the type: integer, null',
157157
);
158158
});
159+
160+
it('throws an exception if the multipleOf is less than 0', function (): void {
161+
Schema::integer('age')
162+
->description('User age')
163+
->multipleOf(-1);
164+
})->throws(SchemaException::class, 'multipleOf must be greater than 0');

tests/Unit/Targets/ObjectSchemaTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,25 @@
173173
'key2' => 'value2',
174174
]))->not->toThrow(SchemaException::class);
175175
});
176+
177+
it('can specify a propertyNames schema', function (): void {
178+
$schema = Schema::object('user')
179+
->properties(
180+
Schema::string('name')->required(),
181+
)
182+
// ->additionalProperties(true)
183+
->propertyNames(Schema::string()->pattern('^[a-zA-Z]+$'));
184+
185+
$schemaArray = $schema->toArray();
186+
187+
expect($schemaArray)->toHaveKey('propertyNames.pattern', '^[a-zA-Z]+$');
188+
189+
// Validation tests
190+
expect(fn() => $schema->validate([
191+
'name' => 'John Doe',
192+
]))->not->toThrow(SchemaException::class);
193+
194+
expect(fn() => $schema->validate([
195+
'name' => 123, // invalid property name pattern
196+
]))->toThrow(SchemaException::class, 'The properties must match schema: name');
197+
});

tests/Unit/Targets/StringSchemaTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@
105105
);
106106

107107
expect(fn() => $schema->validate('test@example.com'))->not->toThrow(SchemaException::class);
108+
109+
$schema = Schema::string('email')
110+
->description('User email address')
111+
->format('custom');
112+
113+
expect($schema->toArray())->toHaveKey('format', 'custom');
108114
});
109115

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

131+
expect($schema->isValid(null))->toBeTrue();
132+
expect($schema->isValid('John'))->toBeTrue();
133+
125134
expect(fn() => $schema->validate(123))->toThrow(
126135
SchemaException::class,
127136
'The data (integer) must match the type: string, null',
128137
);
138+
expect($schema->isValid(123))->toBeFalse();
129139
});
130140

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

0 commit comments

Comments
 (0)