Validator: v.array() weird experience
See original GitHub issueimport { 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 underembeds
- if you don’t it incorrectly looks underresponse
. #683 - 2.
v.array()
won’t accept an empty array - even with it being marked as isOptional, the validator will error. This also occurs whennull
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 puttingresponse.flags
beforeresponse.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:
- Created 10 months ago
- Comments:17 (8 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thank you very much both!
@Skye-31 point 1 should be ok now 🙂👍