Comments (2)
I have come up with this function which passed all tests I had written so far:
export function validateTime(config: Config) {
// defaults
const _required = config.required ?? !config.optional ?? false
return pipe(
string(),
!_required ? minLength(0) : nonEmpty('Required field'),
check((input) => {
if (!_required && !input) return true
return /^(?:0\d|1\d|2[0-3]):[0-5]\d$/u.test(input)
}, 'Invalid time format'),
)
}
But I'd rather use check
as a last resort.
from valibot.
Here are two examples:
import * as v from 'valibot';
// Time or undefined
const Schema1 = v.optional(v.pipe(v.string(), v.isoTime()));
// Time or empty string
const Schema2 = v.union([v.literal(''), v.pipe(v.string(), v.isoTime())]);
from valibot.
Related Issues (20)
- transform does not work with fallback if union is also used... HOT 2
- Help with checking if minimum 1 element of the array doesn't have empty string. HOT 6
- How to create dynamic variants with the nested schemas? HOT 1
- [FEATURE] check and checkAsync can accept message (string) in return HOT 2
- v.record numbers as keys HOT 1
- checkAsync with valibotResolver not working HOT 1
- [feat suggestion]: partial/nullish/nullable object with default values HOT 1
- "Cannot read properties of null" when using trim with nullish HOT 4
- algolia search not working HOT 2
- Preserve `@ts-expect-error` directives HOT 7
- feat: Add TransformStream Validation HOT 1
- Add an example for handling ValiError errors HOT 10
- Using variant with a nested key HOT 1
- Add length validators based on the `Intl.Segmenter` API HOT 1
- What should I put inside the pipe HOT 1
- May we make `function_()` generic? HOT 4
- nested prop of flattened issues is typed as '{} | undefined' when object schema is piped to forward HOT 2
- Nested variants HOT 9
- As of 0.38.0 `transform` breaks `pipe` in some cases HOT 13
- `forward` breaks `pipe` in >=v0.38.x HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from valibot.