-
-
Notifications
You must be signed in to change notification settings - Fork 245
feat: add json string validation action #957
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
base: main
Are you sure you want to change the base?
feat: add json string validation action #957
Conversation
850c22f
to
e0abe52
Compare
Thank you Hendrik! I will review this PR after our v1 RC or stable v1 is out. |
f41426c
to
d713dfe
Compare
e0abe52
to
051ab71
Compare
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
commit: |
051ab71
to
2d3b658
Compare
i think the difficulty with naming is that there's three different things that
In my personal experience I've found the last one to be the most useful, which is why I've opened the PR referenced above. I can see the utility of the other two however, so I think naming would just be important to avoid confusion. I'd generally be against anything that requires parsing the JSON twice and throwing away the first result, which is why I think there's room for both types of action (transform and not transform). v.pipe(
v.string(),
v.jsonString(), // parse happens here, gets thrown away
v.transform(JSON.parse) // parse happens again
); The experience with an action that transforms is quite nice, as you can then pipe it to your final expected type: const prefsSchema = v.object({
theme: v.picklist(["light", "dark"]),
})
type Prefs = v.InferOutput<typeof prefsSchema>
const prefsFromStorageSchema = v.union([
v.pipe(v.string(), v.parsedJson(), prefsSchema),
v.null()
})
const prefsFromStorage = v.parse(prefsFromStorageSchema, localStorage.get("prefs"))
// ^? Prefs | null The question also arises as to how much code the two "parse" actions should share, seeing as they're essentially doing the same thing, one is just using the result and the other is ignoring it. Would it make sense for them to throw the same error? In my personal projects I've actually treated const prefsSchema = v.object({
theme: v.picklist(["light", "dark"]),
})
type Prefs = v.InferOutput<typeof prefsSchema>
const prefsFromStorageSchema = v.union([
v.json(prefsSchema),
v.null()
})
const prefsFromStorage = v.parse(prefsFromStorageSchema, localStorage.get("prefs"))
// ^? Prefs | null But when opening the PR I opted for an action as it felt more consistent with existing code. |
This PR defines a JSON action to validate strings to be valid json strings (not objects).
Recently an Issue was created to document a valibot schema to validate objects to be json compliant. In that PR @fabian-hiller mentioned a validation action to ensure strings are valid JSON. This PR aims to add that action to valibot.
I'm a bit torn if this action should be called
json
orjsonString
. I lean towards just calling itjson
, which is why I went with that.Relevant comment: #933 (comment)
There is another open PR that adds documentation for the valibot schema to validate objects for json compliance here: #936