|
5 | 5 | namespace Cortex\JsonSchema\Tests\Unit; |
6 | 6 |
|
7 | 7 | use Cortex\JsonSchema\Enums\SchemaFormat; |
| 8 | +use Cortex\JsonSchema\Types\ObjectSchema; |
8 | 9 | use Opis\JsonSchema\Errors\ValidationError; |
9 | 10 | use Cortex\JsonSchema\SchemaFactory as Schema; |
10 | 11 | use Cortex\JsonSchema\Exceptions\SchemaException; |
|
196 | 197 | 'name' => 123, // invalid property name pattern |
197 | 198 | ]))->toThrow(SchemaException::class, 'The properties must match schema: name'); |
198 | 199 | }); |
| 200 | + |
| 201 | +it('can create an object schema with pattern properties', function (): void { |
| 202 | + $schema = Schema::object('config') |
| 203 | + ->patternProperty('^prefix_', Schema::string()->minLength(5)) |
| 204 | + ->patternProperties([ |
| 205 | + '^[A-Z][a-z]+$' => Schema::string(), |
| 206 | + '^\d+$' => Schema::number(), |
| 207 | + ]); |
| 208 | + |
| 209 | + $schemaArray = $schema->toArray(); |
| 210 | + |
| 211 | + // Check schema structure |
| 212 | + expect($schemaArray)->toHaveKey('patternProperties'); |
| 213 | + expect($schemaArray['patternProperties'])->toHaveKey('^prefix_'); |
| 214 | + expect($schemaArray['patternProperties'])->toHaveKey('^[A-Z][a-z]+$'); |
| 215 | + expect($schemaArray['patternProperties'])->toHaveKey('^\d+$'); |
| 216 | + |
| 217 | + // Valid data tests |
| 218 | + expect(fn() => $schema->validate([ |
| 219 | + 'prefix_hello' => 'world123', // Matches ^prefix_ and meets minLength |
| 220 | + 'Name' => 'John', // Matches ^[A-Z][a-z]+$ |
| 221 | + '123' => 42, // Matches ^\d+$ |
| 222 | + ]))->not->toThrow(SchemaException::class); |
| 223 | + |
| 224 | + // Invalid pattern property value (too short) |
| 225 | + expect(fn() => $schema->validate([ |
| 226 | + 'prefix_hello' => 'hi', // Matches pattern but fails minLength |
| 227 | + ]))->toThrow(SchemaException::class); |
| 228 | + |
| 229 | + // Invalid pattern property type |
| 230 | + expect(fn() => $schema->validate([ |
| 231 | + '123' => 'not a number', // Matches pattern but wrong type |
| 232 | + ]))->toThrow(SchemaException::class); |
| 233 | +}); |
| 234 | + |
| 235 | +it('throws exception for invalid regex patterns', function (): void { |
| 236 | + $schema = Schema::object('test'); |
| 237 | + |
| 238 | + expect(fn(): ObjectSchema => $schema->patternProperty('[a-z', Schema::string())) |
| 239 | + ->toThrow(SchemaException::class, 'Invalid pattern: [a-z'); |
| 240 | + |
| 241 | + expect(fn(): ObjectSchema => $schema->patternProperties([ |
| 242 | + '^valid$' => Schema::string(), |
| 243 | + '[a-z' => Schema::string(), |
| 244 | + ]))->toThrow(SchemaException::class, 'Invalid pattern: [a-z'); |
| 245 | +}); |
| 246 | + |
| 247 | +it('can combine pattern properties with regular properties', function (): void { |
| 248 | + $schema = Schema::object('user') |
| 249 | + ->properties( |
| 250 | + Schema::string('name')->required(), |
| 251 | + Schema::integer('age')->required(), |
| 252 | + ) |
| 253 | + ->patternProperty('^custom_', Schema::string()) |
| 254 | + ->additionalProperties(false); |
| 255 | + |
| 256 | + $schemaArray = $schema->toArray(); |
| 257 | + |
| 258 | + // Check schema structure |
| 259 | + expect($schemaArray)->toHaveKey('properties'); |
| 260 | + expect($schemaArray)->toHaveKey('patternProperties'); |
| 261 | + expect($schemaArray)->toHaveKey('additionalProperties', false); |
| 262 | + |
| 263 | + // Valid data |
| 264 | + expect(fn() => $schema->validate([ |
| 265 | + 'name' => 'John', |
| 266 | + 'age' => 30, |
| 267 | + 'custom_field' => 'value', |
| 268 | + ]))->not->toThrow(SchemaException::class); |
| 269 | + |
| 270 | + // Missing required property |
| 271 | + expect(fn() => $schema->validate([ |
| 272 | + 'name' => 'John', |
| 273 | + 'custom_field' => 'value', |
| 274 | + ]))->toThrow(SchemaException::class); |
| 275 | + |
| 276 | + // Invalid additional property (doesn't match pattern) |
| 277 | + expect(fn() => $schema->validate([ |
| 278 | + 'name' => 'John', |
| 279 | + 'age' => 30, |
| 280 | + 'invalid_field' => 'value', |
| 281 | + ]))->toThrow(SchemaException::class); |
| 282 | +}); |
0 commit comments