form-builder/form-schema-validator 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

form-builder/form-schema-validator

Composer 安装命令:

composer require form-builder/form-schema-validator

包简介

Lightweight PHP utilities for validating form schemas.

README 文档

README

Lightweight, framework-agnostic validators for the form-builder schema and user submissions.

Requirements

  • PHP ^8.2

Installation

composer require form-builder/form-schema-validator

Local/path install (optional)

If you are developing against this package inside a monorepo, you can install it via a Composer path repository:

{
  "repositories": [
    {
      "type": "path",
      "url": "path/to/form-builder/package/php"
    }
  ],
  "require": {
    "form-builder/form-schema-validator": "*"
  }
}

Usage

API

  • FormSchema\SchemaValidator
    • validate(array $schema): ValidationResult
    • assertValid(array $schema): void
  • FormSchema\SubmissionValidator
    • validate(array $schema, array $payload, array $replacements = [], array $validations = []): ValidationResult
    • assertValid(array $schema, array $payload, array $replacements = [], array $validations = []): void
  • FormSchema\ValidationResult
    • isValid(): bool
    • errors(): array<string, array<int, string>>
    • valid(): array<string, mixed>

Quick start

use FormSchema\SubmissionValidator;

$schema = [
    'form' => [
        'pages' => [
            [
                'sections' => [
                    [
                        'fields' => [
                            [
                                'key' => 'age',
                                'type' => 'number',
                                'required' => true,
                                'constraints' => ['min' => 18],
                            ],
                        ],
                    ],
                ],
            ],
        ],
    ],
];

$payload = ['age' => 17];

$result = (new SubmissionValidator())->validate($schema, $payload);

if (! $result->isValid()) {
    var_dump($result->errors()); // ['age' => ['The age must be at least 18.']]
} else {
  var_dump($result->valid()); // ['age' => 17]
}

Schema validation

use FormSchema\SchemaValidator;

$validator = new SchemaValidator();

$result = $validator->validate($schema);

if (! $result->isValid()) {
    // handle $result->errors()
}

// or throw on failure
$validator->assertValid($schema);

The validator checks that pages, sections, and fields exist with supported field types, and that option fields define options. Extend it to fit additional rules as your schema evolves.

Submission validation

Validate a user-submitted payload against a schema (field validations + required flags + field constraints):

use FormSchema\SubmissionValidator;

$validator = new SubmissionValidator();

$schema = [/* form schema with pages/sections/fields */];
$payload = request()->all();
$context = ['external_token' => 'abc123']; // optional replacements merged (and overriding) before validation

$result = $validator->validate($schema, $payload, $context);

if (! $result->isValid()) {
    // handle $result->errors()
}

// Optional: pass runtime validation extensions
$runtimeValidations = [
  'fields' => [
    'username' => [
      'required',
      ['rule' => 'starts_with', 'params' => ['USR-']],
      static fn (array $context) => $context['validator']('regex', '^[A-Z0-9-]+$'),
    ],
  ],
  'rules' => [
    // map a custom schema rule name to a callable resolver
    'corp_domain' => static fn (array $context) => $context['validator'](
      'email_domains',
      [(string) ($context['params'][0] ?? '')],
      [],
    ),
  ],
];

$result = $validator->validate($schema, $payload, $context, $runtimeValidations);

Runtime validation extensions

You can optionally pass a 4th argument to validate() / assertValid():

$validator->validate($schema, $payload, $replacements, $runtimeValidations);
  • fields: map of field key to extra validations.
  • rules: map of schema rule name to callable resolver.

fields[fieldKey] entries can be:

  • a string rule name (e.g. required)
  • a schema-like array: ['rule' => 'min', 'params' => [3], 'message' => '...']
  • a bare callable (direct value validator)
  • a callable-validator entry: ['validate' => callable, 'message' => '...']
  • a resolver entry: ['resolver' => callable] for rule-mapping behavior

For bare callables (or ['validate' => callable]), the callable receives the same context array and may return:

  • true / null: pass
  • false: fail (uses provided message or default)
  • string: fail with that message
  • ['valid' => bool, 'message' => string|null]: explicit result payload

Use ['resolver' => callable] when the callable should return mapped validation rules (Rule, string rule, array of rules, or null) instead of pass/fail.

Rule resolver callables receive a context array with:

  • validator, schema, payload, replacements, data
  • field_key, field, rule, params, entry
  • default_rule_mapper (closure for mapping to built-in rules)

Each field may define validations as an array of rules:

'validations' => [
    ['rule' => 'min', 'params' => [3], 'message' => 'Must be at least 3.'],
    ['rule' => 'required_if', 'params' => ['other_field', true], 'message' => 'Required when other_field is true.'],
],

All rules (except required and the required_* variants) treat empty values as "pass" (i.e. optional fields only validate when present).

Field references in rule params

Some rules support pointing at another field value instead of a literal:

['rule' => 'gt', 'params' => ['{field:min_age}']],

The {field:...} syntax is also supported for before / after.

Field constraints

In addition to validations, the validator will also enforce constraints by field type, for example:

  • Text-like fields (short-text, text, medium-text, long-text, address) validate constraints.min_length / constraints.max_length
  • number validates constraints.min / constraints.max / constraints.step
  • tag validates constraints.min / constraints.max as tag count
  • email validates domain allow/deny lists via constraints.allowed_domains / constraints.disallowed_domains (and constraints.max_length)
  • country validates constraints.allow_countries / constraints.exclude_countries
  • phone may include constraints.default_country as a renderer hint for pre-selecting a country
  • File inputs (file, image, video, document) validate constraints.accept, allow_multiple, min, max, max_file_size, max_total_size

Supported field validations

Rule Params Notes
required Value must be non-empty (null, '', [] are empty).
required_if [otherKey, otherValue, ...] Required when context[otherKey] == any provided value.
required_unless [otherKey, otherValue, ...] Required unless context[otherKey] == any provided value.
required_if_accepted [otherKey] Required when context[otherKey] is accepted (true, 1, '1', 'true', 'on', 'yes').
required_if_declined [otherKey] Required when context[otherKey] is declined (false, 0, '0', 'false', 'off', 'no').
required_with [otherKey, ...] Required when any referenced context key is non-empty.
required_with_all [otherKey, ...] Required when all referenced context keys are non-empty.
required_without [otherKey, ...] Required when any referenced context key is empty.
required_without_all [otherKey, ...] Required when all referenced context keys are empty.
email Valid email (FILTER_VALIDATE_EMAIL).
phone Matches /^[0-9 +().-]{6,}$/.
boolean Accepts true/false, 0/1, '0'/'1', 'true'/'false', 'yes'/'no', 'on'/'off', 'y'/'n'.
string Must be a string.
numeric Must be numeric (is_numeric).
gt [target] Number must be > target (supports {field:otherKey}).
gte [target] Number must be >= target (supports {field:otherKey}).
lt [target] Number must be < target (supports {field:otherKey}).
lte [target] Number must be <= target (supports {field:otherKey}).
min [min] Numbers: >= min. Strings: length >= min.
max [max] Numbers: <= max. Strings: length <= max.
between [min, max] Inclusive range for numbers or string length.
not_between [min, max] Outside inclusive range for numbers or string length.
in [value, ...] Strict match (in_array(..., true)).
not_in [value, ...] Strict non-match.
date Validates Y-m-d by default.
time Validates HH:mm or HH:mm:ss.
datetime Validates YYYY-MM-DDTHH:mm (or seconds) and YYYY-MM-DD HH:mm variants.
before [date] strtotime comparison vs target date (supports {field:otherKey} as the date source).
after [date] strtotime comparison vs target date (supports {field:otherKey} as the date source).
regex [pattern] preg_match(pattern, value) === 1 (supports both delimited regex and plain patterns such as ^[A-Z0-9]+$).
starts_with [prefix, ...] String must start with any provided prefix.
ends_with [suffix, ...] String must end with any provided suffix.

Option fields must provide option_properties.data.

Testing

composer test

License

MIT

form-builder/form-schema-validator 适用场景与选型建议

form-builder/form-schema-validator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 51 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 03 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 form-builder/form-schema-validator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 form-builder/form-schema-validator 我们能提供哪些服务?
定制开发 / 二次开发

基于 form-builder/form-schema-validator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 51
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 19
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-03-25