Comments (6)
Thank you for the reply. Anyhow, this issue is fully resolved so I'm closing it.
from valibot.
Instead of a string, you can also pass a function to any schema and validation action to dynamically generate the error message. The first argument of this function gives you access to the issue
object. Also, I will soon start working on a refine
action (issue #597) that will give you full control for such advanced cases.
const Schema = v.string((issue) => `Expected: ${issue.expected}`);
from valibot.
Thanks you for the heads up. However, this way of dynamically generating messages is quite limited. What I mean is, arguably, more often than not these dynamic messages will be based on validation context. In my example above, the message depends on extracted characters. This way, I basically need to repeat the logic twice, first in check
and then in the message generator.
In my real life use case, I am validating a textarea value which is supposed to store the whitespace delimited list of wallets where each wallet must be a valid base58-encoded public key. The validation is supposed to report the particular malformed string, or which keys are duplicate, or when it's not enough or too many of them and why — all and all, that are mostly context-dependent messages.
I hope refine
will make this possible!
from valibot.
Please have a look if rawCheck
solves this issue for you: #597 (comment)
from valibot.
It does:
const schema = v.pipe(v.string(), v.rawCheck(({ dataset, addIssue }) => {
if (dataset.typed) {
const [c1, c2, c3] = dataset.value
if (c1 === "f" && c2 !== c3) {
addIssue({ message: `Since the first letter is "${c1}", the second and third letters ("${c2}" and "${c3}") should equal.` })
}
}
}))
I'm still not really following why do we need the obligatory if (dataset.typed)
boilerplate in each and every check, but at least it gets things done, thank you.
from valibot.
It does
I think I was able to simplify your scheme. However, your version is probably a bit more readable.
import * as v from 'valibot';
const Schema = v.pipe(
v.string(),
v.check(
([c1, c2, c3]) => c1 !== 'f' || c2 == c3,
({ input: [c1, c2, c3] }) =>
`Since the first letter is "${c1}", the second and third letters ("${c2}" and "${c3}") should equal.`,
),
);
I'm still not really following why do we need the obligatory
if (dataset.typed)
boilerplate in each and every check, but at least it gets things done, thank you.
This is because I am working on validation actions that allow you to validate untyped data if the part of the data you want to validate is typed. This is really important for form validation. Without this functionality, we can't show errors that depend on multiple fields if any other field is untyped.
from valibot.
Related Issues (20)
- As of 0.38.0 `transform` breaks `pipe` in some cases HOT 13
- `forward` breaks `pipe` in >=v0.38.x HOT 11
- `GenericSchema` where `TOutput` does not default to `TInput` HOT 2
- bug: `transform` output type inside an object is broken on v0.39 HOT 3
- Adding labels to values in issue paths HOT 1
- Return type of `getDotPath` function is `null` for schemas with `pipeAsync` HOT 3
- How to create a typescript type for an arg of type v.object or v.pipe ? HOT 3
- Add clear property to indicate invalid property path HOT 6
- Validation Recheck Not Triggered for Other Indices or Fields in Array When Using forward with a Specific Index HOT 1
- Decimal doesn't support non-integer numbers HOT 2
- Returned Type of startsWith is still string HOT 1
- `rawCheck` accepts a function that returns a `Promise` HOT 4
- Pipe for actions/checks without schema HOT 5
- Move Explanation to top in Api docs HOT 1
- Is it possible to create an error/warning on a field which is non blocking? HOT 1
- Is there a way to get the metadata as type? HOT 1
- Is it possible to combine `GenericSchema` with `variant`? HOT 3
- ValibotSchema to FormikValidationSchema HOT 5
- Error: The "nanoid" action cannot be converted to JSON Schema. HOT 2
- `NoInfer` on check Input?
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.