Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

hendrikheil
Copy link
Contributor

@hendrikheil hendrikheil commented Dec 4, 2024

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 or jsonString. I lean towards just calling it json, 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

@hendrikheil hendrikheil force-pushed the feat/json-string-validation branch from 850c22f to e0abe52 Compare December 4, 2024 09:42
@fabian-hiller fabian-hiller self-assigned this Dec 5, 2024
@fabian-hiller fabian-hiller added the enhancement New feature or request label Dec 5, 2024
@fabian-hiller
Copy link
Owner

Thank you Hendrik! I will review this PR after our v1 RC or stable v1 is out.

Copy link

vercel bot commented Feb 24, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
valibot ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 7, 2025 2:47pm

Copy link

pkg-pr-new bot commented Feb 24, 2025

Open in StackBlitz

npm i https://pkg.pr.new/valibot@957

commit: 2d3b658

@EskiMojo14
Copy link
Contributor

EskiMojo14 commented Apr 8, 2025

i think the difficulty with naming is that there's three different things that v.json could do:

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 .json more like a method/schema:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants