Skip to content

Commit 729bebb

Browse files
committed
fix
1 parent 9b32c0b commit 729bebb

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/Types/StringSchema.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ public function pattern(string $pattern): static
7979
*/
8080
public function contentEncoding(string $contentEncoding): static
8181
{
82+
$this->validateFeatureSupport(SchemaFeature::ContentEncoding);
8283
$this->contentEncoding = $contentEncoding;
8384

8485
return $this;
@@ -89,6 +90,7 @@ public function contentEncoding(string $contentEncoding): static
8990
*/
9091
public function contentMediaType(string $contentMediaType): static
9192
{
93+
$this->validateFeatureSupport(SchemaFeature::ContentMediaType);
9294
$this->contentMediaType = $contentMediaType;
9395

9496
return $this;

tests/Unit/VersionFeatureValidationTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,32 @@
411411
expect($emailFormatFeatures)->toBeEmpty();
412412
});
413413

414+
it('validates content encoding and media type features against schema version', function (): void {
415+
// Draft 06 should reject contentEncoding/contentMediaType
416+
$stringSchema = Schema::string('test', SchemaVersion::Draft_06);
417+
418+
expect(fn(): StringSchema => $stringSchema->contentEncoding('base64'))->toThrow(
419+
SchemaException::class,
420+
'Feature "Content encoding annotation" is not supported in Draft 6. Minimum version required: Draft 7.',
421+
);
422+
expect(fn(): StringSchema => $stringSchema->contentMediaType('application/json'))->toThrow(
423+
SchemaException::class,
424+
'Feature "Content media type annotation" is not supported in Draft 6. Minimum version required: Draft 7.',
425+
);
426+
427+
// Draft 07+ should accept contentEncoding/contentMediaType
428+
$draft07Schema = Schema::string('test', SchemaVersion::Draft_07);
429+
$draft201909Schema = Schema::string('test', SchemaVersion::Draft_2019_09);
430+
$draft202012Schema = Schema::string('test', SchemaVersion::Draft_2020_12);
431+
432+
expect(fn(): StringSchema => $draft07Schema->contentEncoding('base64'))->not->toThrow(SchemaException::class);
433+
expect(fn(): StringSchema => $draft07Schema->contentMediaType('application/json'))->not->toThrow(SchemaException::class);
434+
expect(fn(): StringSchema => $draft201909Schema->contentEncoding('base64'))->not->toThrow(SchemaException::class);
435+
expect(fn(): StringSchema => $draft201909Schema->contentMediaType('application/json'))->not->toThrow(SchemaException::class);
436+
expect(fn(): StringSchema => $draft202012Schema->contentEncoding('base64'))->not->toThrow(SchemaException::class);
437+
expect(fn(): StringSchema => $draft202012Schema->contentMediaType('application/json'))->not->toThrow(SchemaException::class);
438+
});
439+
414440
it('allows string formats for custom validation', function (): void {
415441
// String formats should not trigger validation (for custom formats)
416442
$stringSchema = Schema::string('test', SchemaVersion::Draft_07);

0 commit comments

Comments
 (0)