question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Validator: v.array() weird experience

See original GitHub issue
import { Hono } from "hono";
import { validator } from "hono/validator";

const app = new Hono();

app.post(
	"/",
	validator(
		(v) => ({
			response: v.object("response", (v1) => ({
				content: v1.json("content").isOptional().isLength({ max: 2000 }),
				embeds: v1
					.array("embeds", (v2) => ({
						title: v2.json("title").isOptional(),
						description: v2.json("description").isOptional(),
						url: v2.json("url").isOptional(),
						timestamp: v2.json("timestamp").isOptional(),
						color: v2.json("color").asNumber().isOptional(),
						author: v2
							.object("embeds.[*].author", (v3) => ({
								name: v3.json("name").isOptional(),
								url: v3.json("url").isOptional(),
								icon_url: v3.json("icon_url").isOptional(),
							}))
							.isOptional(),
						footer: v2
							.object("embeds.[*].footer", (v3) => ({
								text: v3.json("text").isOptional(),
								icon_url: v3.json("icon_url").isOptional(),
							}))
							.isOptional(),
						image: v2
							.object("embeds.[*].image", (v3) => ({
								url: v3.json("url").isOptional(),
							}))
							.isOptional(),
						thumbnail: v2
							.object("embeds.[*].thumbnail", (v3) => ({
								url: v3.json("url").isOptional(),
							}))
							.isOptional(),
						fields: v2
							.array("embeds.[*].fields", (v3) => ({
								name: v3.json("name"),
								value: v3.json("value"),
								inline: v3.json("inline").isOptional().asBoolean(),
							}))
							.isOptional(),
					}))
					.isOptional(),
				flags: v1.json("flags").isOptional().asNumber(),
			})),
		}),
		{
			done: (res) => {
				if (res.hasError) {
					return new Response(res.messages.join("\n"), { status: 400 });
				}
			},
		}
	),
	(c) => {
		return Response.json(c.req.valid());
	}
);

app.notFound(() => {
	return new Response("Not found", { status: 404 });
})

export default app;

Issues:

  • 1. I have to use fields: v2.array("embeds.[*].fields") despite the fact it’s already under embeds - if you don’t it incorrectly looks under response. #683
  • 2. v.array() won’t accept an empty array - even with it being marked as isOptional, the validator will error. This also occurs when null is provided. #690
  • 3. "Invalid Value [64]: the JSON body \"response.flags\" is invalid - should be \"number[]\"" - this is not under an array field - it should not be counted as an array? It turns out this can be fixed by putting response.flags before response.embeds in the validator - this is odd. #680

I appreciate this issue description is pretty awful - but I feel like I’m running into a combination of bugs here with nested arrays and objects

Issue Analytics

  • State:closed
  • Created 10 months ago
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
Skye-31commented, Nov 30, 2022

Thank you very much both!

2reactions
dario-piotrowiczcommented, Nov 29, 2022

@Skye-31 point 1 should be ok now 🙂👍

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Validate an Array of Objects and Sub-Objects using ...
I am trying to validate the data being passed with an array of objects in a JSON request. I used express-validator wildcards as...
Read more >
Weird validation where field is an array · Issue #883 · express ...
Both examples validate without error. Doesn't matter the values are array, it seems to only check if the first element is valid.
Read more >
Weird recursion with Validation - FuelPHP forums
Hi! I'm having a weird problem with the Validation. My errors array explodes in size due to recursion and I can't for the...
Read more >
Array validation does not work if they are files - Laracasts
The validation always fails. The page always redirects and says the field is required even though it is filled. If I change the...
Read more >
why validation does not work in my code?
Always returning an array of 0, 1 or more entries would be more helpful and the addition of a findAll method for that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found