Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: typed validators #118

Merged
merged 16 commits into from
Nov 8, 2024
Merged

feat!: typed validators #118

merged 16 commits into from
Nov 8, 2024

Conversation

ssaarela
Copy link
Collaborator

@ssaarela ssaarela commented Oct 28, 2024

New Features

  • Typing: Validators may have a specific input and especially output type.
  • V.objectType() builder can be used to build an ObjectValidator with inferred type.
  • Validator (output/result) type can be acquired with VType<typeof validator>.
  • Direct, chainable support for most used "next" validation rules (e.g. V.number().min(1).max(2)):
    • V.string() supports notEmpty, notBlank and pattern.
    • V.number() supports min and max.

Breaking changes:

  • V.string() and some other validators do not support String object as input any more.
  • V.number() does not support Number object as input any more.
  • Validators that accept multiple subvalidators (V.optional, V.required, V.check, V.if, V.whenGroup, V.json and ObjectModel#next) are combined using V.compositionOf instead of V.allOf as composition makes more sense in general. However, if there are multiple parents with next validators, those are still combined with V.allOf as they are not aware of each other.
  • Internal Validator#validatePath returns now a promise (success, or reject) of value directly instead of ValidationResult

Copy link

codecov bot commented Oct 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (24a7a43) to head (c19dbda).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##            master      #118    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files           16        18     +2     
  Lines         3921      4182   +261     
  Branches       638       659    +21     
==========================================
+ Hits          3921      4182   +261     
Files with missing lines Coverage Δ
packages/core/src/V.ts 100.00% <100.00%> (ø)
packages/core/src/objectValidator.ts 100.00% <100.00%> (ø)
packages/core/src/objectValidatorBuilder.ts 100.00% <100.00%> (ø)
packages/core/src/schema.ts 100.00% <100.00%> (ø)
packages/core/src/validators.ts 100.00% <100.00%> (ø)
packages/luxon/src/Vluxon.ts 100.00% <100.00%> (ø)
packages/moment/src/Vmoment.ts 100.00% <100.00%> (ø)

@ssaarela ssaarela changed the title feat: typed validators (simple cases) feat: typed validators Nov 4, 2024
Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
* initial object type builder
* number & string validator chaining

Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
@ssaarela ssaarela marked this pull request as ready for review November 5, 2024 16:05
@ssaarela ssaarela changed the title feat: typed validators feat!: typed validators Nov 5, 2024
Copy link

@finnair-jw finnair-jw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typing the existing validators is a large effort. This approach seems quite reasonable. Getting it to this point and releasing it as a beta version was a good idea.

As a general comment for the next updates, in a lot of cases the validators are still using any for the input value. unknown would behave the same way for the caller, and be much safer inside the validator function.

packages/core/src/schema.ts Outdated Show resolved Hide resolved
packages/core/src/validators.ts Outdated Show resolved Hide resolved
Breaking change: V.number() does not support Number object any more

Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
* ObjectValidators parent next validators as allOf
* V.array, V.if and V.whenGroup validators with multiple (compositionOf) item validators

Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
…lidationResult wrapper

* Validator#validatePath either returns Promise.resolve(value) or reject with violation(s)
* This allows better type inference as failure case doesn't mix with success case

Signed-off-by: Samppa Saarela <samppa.saarela@iki.fi>
@ssaarela ssaarela merged commit 0331072 into master Nov 8, 2024
7 checks passed
@ssaarela ssaarela deleted the f/result-typing branch November 8, 2024 11:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants