Skip to content

feat: describe the rules by the keywords of JSON Schema - #70

Merged
Insei merged 1 commit into
mainfrom
feat/constraints
Oct 19, 2026
Merged

Insei merged 1 commit into
mainfrom
feat/constraints

Conversation

@Insei

@Insei Insei commented Oct 19, 2026

Copy link
Copy Markdown
Owner

Validator.Constraints() returns the rules that JSON Schema can express, with the struct, the field, the path (items, additionalProperties, propertyNames), the keyword and the value — for the generators of API schemas; the huma module of the next pull request puts them into the OpenAPI document.

Rules Keywords
str: Required, MinRunes, MaxRunes, MaxLen required + minLength: 1, minLength, maxLength (MinLen in bytes isn't described: the schema would be stricter than the rule)
str: Regexp, Prefix, Suffix, Contains, Alphanumeric, Semver, E164, HexColor pattern
str: Email, URL, IPv4, IPv6, Hostname, UUID, Datetime(RFC3339 / DateOnly) format
str: AnyOf, num/uuid: AnyOf, eq: OneOf, Equal enum, const
num: Min, Max, AnyOfInterval, Latitude, Longitude minimum, maximum
slices: Required, MinLen, MaxLen, Unique, Each minItems, maxItems, uniqueItems, items
maps: Required, MinLen, MaxLen, EachKey, EachValue minProperties, maxProperties, propertyNames, additionalProperties

Conditional rules (When of the struct and of fields) aren't described; after Modify (e.g. Trim) only Required is, the schema describes the raw value. Field.Schema / SliceFieldConfigurator.Schema describe the rules of your conversions. The constructors NewValueField, NewSliceFieldConfigurator, NewMapFieldConfigurator take the registration of the constraints (nil for none) — not released yet, v0.1.0 isn't tagged. Validation is unchanged: the constraints are collected by Configure only.

Validator.Constraints returns the rules that JSON Schema can express,
maxLength of MaxRunes, minimum of Min, enum of AnyOf, pattern, format,
items of Each..., with their fields, for the generators of the schemas
of the API. The rules under When aren't described, after Modify only
Required is. Field.Schema and SliceFieldConfigurator.Schema describe
the rules of the conversions; the constructors of the configurators
take the registration of the constraints.
@github-actions

Copy link
Copy Markdown
Contributor

Benchmark diff: a7612d7 → 0edba32 (10 runs)

goos: linux
goarch: amd64
pkg: github.com/insei/valigo
cpu: AMD EPYC 7763 64-Core Processor                
           │  base.txt   │              head.txt              │
           │   sec/op    │   sec/op     vs base               │
Validate-4   144.8n ± 1%   148.2n ± 1%  +2.38% (p=0.000 n=10)

           │  base.txt  │            head.txt            │
           │    B/op    │    B/op     vs base            │
Validate-4   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

           │  base.txt  │            head.txt            │
           │ allocs/op  │ allocs/op   vs base            │
Validate-4   0.000 ± 0%   0.000 ± 0%  ~ (p=1.000 n=10) ¹
¹ all samples are equal

@Insei
Insei merged commit d14ecaf into main Oct 19, 2026
6 checks passed
@Insei
Insei deleted the feat/constraints branch October 19, 2026 08:20
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (a7612d7) to head (9dda86d).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main       #70   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           35        36    +1     
  Lines         1110      1189   +79     
=========================================
+ Hits          1110      1189   +79     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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.

2 participants