Skip to content

feat!: validators declare the types they apply to - #105

Merged
Kinflou merged 1 commit into
masterfrom
feat/validator-targets
Oct 18, 2026
Merged

Kinflou merged 1 commit into
masterfrom
feat/validator-targets

Conversation

@Kinflou

@Kinflou Kinflou commented Oct 18, 2026

Copy link
Copy Markdown
Member

Core part of #97. Stacked on #104 (std's validators need string); GitHub retargets this to master when #104 merges.

Syntax

validator StringBounds for string { ... }
validator Range for u8, u16, u32 { ... }

Checks (all build errors)

Code When
validation::validator-missing-target a validator has no for. The grammar keeps for optional only so this can name what's missing, with a fix-it hint.
validation::validator-target-mismatch at the field: "validator 'StringBounds' applies to string, not s32". Exact match after resolving aliases ("not Count (u32)"). A validator on string[] checks the array, not each element. optional doesn't matter.
validation::validator-accessor-unsupported value.length on a target without a length, or a value accessor other than length / name

Imported validators and aliases

These were skipped entirely before: the declaring schema isn't among the importing schema's units. validate_with(units, &dyn ImportLookup) lets the caller answer, following the same split as size.rs's SizeLookup:

  • Build: ProjectImports follows the schema's use statements through the ProjectContext, std included.
  • validate(): knows no imports, so behaviour there is unchanged.
  • Language server: implements the trait in its own PR, using the shared declared_validator_targets / declared_alias_target.

Also

  • for is a keyword (vocabulary: 25 keywords).
  • std's StringBounds is now for string.
  • Existing validator fixtures gain for string.
  • Tests cover a missing target; mismatches on a local validator, through an alias, on an array, and with a list of targets; optional and aliased fields that match; both accessor errors; and the cross-schema cases (std's StringBounds, plus an alias and a validator from another schema in the same package).

`validator NAME for TYPE[, TYPE]* { ... }`. The targets freeze onto
FrozenUnit::Validator (`targets`, serde-default), and validation now:

- rejects a validator without `for` (validation::validator-missing-
  target). The grammar keeps `for` optional only so this can say what's
  missing instead of the parser's generic error;
- rejects a field whose type isn't one of its validator's targets
  (validation::validator-target-mismatch), matching exactly after
  resolving aliases - a validator on `string[]` checks the array, not
  each element; `optional` is already outside the frozen type;
- rejects `value.length` on a target without a length, and any `value`
  accessor other than `length`/`name` (validation::validator-accessor-
  unsupported).

Imported validators and aliases are checked too, through a caller-
supplied ImportLookup (validate_with): the build answers from its
ProjectContext via ProjectImports, which follows the schema's `use`
statements (std included); plain validate() knows no imports, as before.
std's StringBounds is now `for string`.

Refs #97
@Kinflou
Kinflou deleted the branch feat/string-only October 18, 2026 01:09
@Kinflou Kinflou closed this Oct 18, 2026
@Kinflou Kinflou reopened this Oct 18, 2026
@Kinflou
Kinflou changed the base branch from feat/string-only to master October 18, 2026 01:09
@Kinflou
Kinflou merged commit 1245d12 into master Oct 18, 2026
9 checks passed
@Kinflou
Kinflou deleted the feat/validator-targets branch October 18, 2026 01:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant