Skip to content

Create Dedicated Constraint Attributes for JSON Schema Validation #16

@butschster

Description

@butschster

String with Pattern and Length

final class User
{
    public function __construct(
        #[Field(title: 'Full Name', description: 'User full name in Title Case')]
        #[Pattern('^[A-Z][a-z]+(?: [A-Z][a-z]+)*$')]
        #[Length(min: 2, max: 100)]
        public readonly string $name,
        
        #[Field(title: 'Email', format: Format::Email)]
        #[Pattern('^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$')]
        public readonly string $email,
    ) {}
}

Numeric with Range

final class Product
{
    public function __construct(
        #[Field(title: 'Price', description: 'Product price in USD')]
        #[Range(min: 0.01, max: 99999.99)]
        #[MultipleOf(0.01)]
        public readonly float $price,
        
        #[Field(title: 'Stock Quantity')]
        #[Range(min: 0, max: 10000)]
        public readonly int $stock,
    ) {}
}

Array with Items Constraint

final class Post
{
    public function __construct(
        #[Field(title: 'Tags', description: 'Post tags')]
        #[Items(min: 1, max: 10, unique: true)]
        public readonly array $tags,
        
        #[Field(title: 'Status')]
        #[Enum(['draft', 'published', 'archived'])]
        public readonly string $status,
    ) {}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions