-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
z.string().url() seems to accept any string #2236
Comments
This is working fine for me, I'm getting the import { z } from 'zod';
const schema = z.object({
projectId: z.string().uuid(),
title: z.string().min(5).max(100),
description: z.string().max(255).optional(),
punchTypeId: z.string().uuid(),
hours: z.number().min(0.25).max(24),
ticketLink: z.string().url(),
billable: z.boolean().optional(),
date: z.date(),
});
const formState = {
date: '2023-03-23T20:00:05.466Z',
projectId: '28dfcf92-5b36-4976-1d48-08db1f3ca426',
ticketLink: 'abc',
};
const result = schema.safeParse(formState);
if (!result.success) console.log(result.error.issues); |
Everything is working as expected for me. Please send a reproducible example if you are still having issues. const schema = z.object( {
ticketLink: z.string().url(),
} )
const formState = {
ticketLink: 'abc',
}
const result = schema.safeParse( formState )
!result.success && console.log( result.error.issues )
// [
// {
// validation: 'url',
// code: 'invalid_string',
// message: 'Invalid url',
// path: [ 'ticketLink' ]
// }
// ] |
Thank you both for your response ! It is not impossible that there is a mistake in my code or something else I haven't setup properly... I will retry and keep you updated. |
I re-verified my code and can't understand why it still seems to skip the validation of this field. Here's the real code snippet used in my app where I added some export const punchCreationFormValidationSelector = selector({
key: "punchCreationFormValidationSelector",
get: ({ get }) => {
const formState = get(punchCreationFormStateAtom);
const schema = z.object({
projectId: z.string({required_error: "Ce champ est requis"}).uuid("Identifiant du projet invalide"),
title: z.string({required_error: "Ce champ est requis"}).trim().min(5, "Le titre du punch doit avoir au moins 5 caractères").max(100, "Le titre du punch doit être inférieur à 100 caractères"),
description: z.string().max(255, "La description du punch doit être inférieure à 255 caractères").optional(),
punchTypeId: z.string({required_error: "Ce champ est requis"}).uuid("Identifiant du type de punch invalide"),
hours: z.coerce.number({required_error: "Ce champ est requis", invalid_type_error: "Ce champ est requis"}).min(0.25, "Le nombre d'heures doit être supérieur à 0.25").max(24, "Le nombre d'heures doit être inférieur à 24"),
ticketLink: z.string().url(),
billable: z.boolean(),
date: z.date({required_error: "Ce champ est requis"}),
})
console.log("Form state for validation", formState)
const result = schema.safeParse(formState);
console.log("result.success", result.success);
if (!result.success) {
const errors = result.error.flatten();
console.log(errors);
return {
success: false,
fieldErrors: errors.fieldErrors,
data: undefined,
}
} else {
return {
success: true,
fieldErrors: undefined,
data: result.data,
}
}
},
}); As we can see in the console output, the However, what's weird is that I tested essentially the same code in a standalone js file with the same version of Zod (outside the React app) and it works properly. zodtest.mjs import { z } from "zod";
const schema = z.object({
projectId: z.string({required_error: "Ce champ est requis"}).uuid("Identifiant du projet invalide"),
title: z.string({required_error: "Ce champ est requis"}).trim().min(5, "Le titre du punch doit avoir au moins 5 caractères").max(100, "Le titre du punch doit être inférieur à 100 caractères"),
description: z.string().max(255, "La description du punch doit être inférieure à 255 caractères").optional(),
punchTypeId: z.string({required_error: "Ce champ est requis"}).uuid("Identifiant du type de punch invalide"),
hours: z.coerce.number({required_error: "Ce champ est requis", invalid_type_error: "Ce champ est requis"}).min(0.25, "Le nombre d'heures doit être supérieur à 0.25").max(24, "Le nombre d'heures doit être inférieur à 24"),
ticketLink: z.string().url(),
billable: z.boolean(),
date: z.date({required_error: "Ce champ est requis"}),
});
const data = {
"billable": true,
"date": new Date("2023-03-20"),
"hours": "1",
"projectId": "28dfcf92-5b36-4976-1d48-08db1f3ca426",
"punchTypeId": "e1a28cff-b39e-4d5f-f0d6-08db2c16a4af",
"ticketLink": "notanurl",
"title": "Punch title"
}
console.log(data);
const result = schema.safeParse(data);
console.log(result.success);
console.log(result.error.flatten().fieldErrors); So, I don't know if the issue is related to Zod or if it relates to recoil, but it's strange since all other fields are validated properly in both scenarios. |
Another interesting thing I found is that if I add other validation rules to For instance, Edit : using a |
@obrassard this is definitely a React Native I couldn't find an upstream tracker issue - maybe that's something you could help us hunt down?: |
Is there any update on this matter ? Or could you provide a specific way to handle this case on native with zod ? Thanks in advance. |
Still facing this issue. |
+1 can confirm it does not work in react native |
+1 can confirm it does not work in react native also |
I have the same problem in react native |
I have the same issue in Vue 3 |
Are any corrections expected, or is it better to use some custom validation? |
Using: "zod": "^3.22.4" Returns Valid: Returns Invalid: Have reverted to using regex for urls for now. |
Additional hostname validation solves most of edge cases but it prevents from using localhost and custom aliases. |
Till then const schema = z.object({
meetingLink: z
.string()
.refine((value) => /^(https?):\/\/(?=.*\.[a-z]{2,})[^\s$.?#].[^\s]*$/i.test(value), {
message: 'Please enter a valid URL',
}),
}); |
I am seeing this issue in Vue3, as well. This URL is valid: This URL (note the leading Version:
I am using this through @vee-validate/zod and this is the validation rule:
|
Almost perfect... when I input "https://", it accepts :( |
This worked for me: |
Hiya, also having the same issue. :( |
I'm having the same issue with react 18.2.0 (no react native) & zod 3.22.2 |
this works for me |
Hello, I think I may have found a bug with URL validation.
Indeed, I am trying to validate an object which has a property “ticketLink" that should be an URL (z.string().url())
This is my schema :
However, no matter what I am setting as a value for the
ticketLink
property, the schema does not return an error for this property, even if the value is not a valid URL.For instance, validating this object :
doesn't raise an error for the
ticketLink
field, but "abc" should not be accepted as an URL.We're using "zod": "3.21.4"
The text was updated successfully, but these errors were encountered: