Comments (2)
The coerce
method has been removed because I felt it was an insecure API. In most cases, you don't want to coerce an unknown input into a specific data type. Instead, you want to transform a specific data type into another specific data type. For example, a string or a number into a date. To explicitly define the input type, we recommend using the new pipe
method together with the transform
action to achieve the same functionality.
// Before
const DateSchema = v.coerce(v.date(), (input) => new Date(input));
// After
const DateSchema = v.pipe(
v.union([v.string(), v.number()]), // <-- Select which input types to accept
v.transform((input) => new Date(input))
);
You can also accept unknown
to have the same behavior as with the older versions.
const DateSchema = v.pipe(
v.unknown()
v.transform((input) => new Date(input))
);
from valibot.
I always appreciate a quick response.
Using unknown
is a good workaround. I modified the code I presented in this way to get the expected behavior.
describe('sample', () => {
it('should throw an error when the string is empty', () => {
// When using version 0.30.0 of valibot
// const schema = coerce(string(), (value) => value === '' ? undefined : value)
// When using version 0.31.0-rc.1 of valibot
const schema = pipe(unknown(), transform((value) => value === '' ? undefined : value), string())
const result = safeParse(schema, '')
expect(result.success).toBe(false)
})
})
I will modify my library based on this code.
Thank you so much for your response. I hope Valibot, a great library, will be more widely adopted.
from valibot.
Related Issues (20)
- Predefined keys for `ObjectSchema` HOT 4
- Automatic upgrade to v0.31.0 with Grit only works for certain patterns HOT 7
- Codemod skipping certain schema HOT 6
- Codemod - missing comma before `v.check` on `v.intersect` HOT 1
- Error while downloading codemod HOT 2
- Form submission proceeds despite validation errors using check in Valibot, causing formValue to be lost HOT 4
- Codemod using `unknown()` schema HOT 3
- Unable to enter multiple paths to forward method HOT 6
- "Unsafe assignment of an `any` value" for pipe method HOT 1
- `ObjectSchema` does not satisfy predefined type (v.0.31) HOT 2
- High CPU usage after upgrading to v0.31.1 HOT 2
- Add a fallback override for `pipe` method to accept unlimited pipe items HOT 7
- Conditionally validate using pipes HOT 1
- Questions on how to proceed HOT 4
- Custom (runtime) error message HOT 6
- Issue with enums HOT 5
- Date validation default error message HOT 5
- Suggestion to Use nil Instead of null_ for Primitive Value HOT 5
- Bi-directional encoding/decoding
- TypeScript compile fails HOT 3
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.