Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
136
rated 0 times [  141] [ 5]  / answers: 1 / hits: 15961  / 2 Years ago, fri, september 2, 2022, 12:00:00

I have a field where I want the value to either be optional OR have the field have a minimum length of 4.


I've tried the following:


export const SocialsSchema = z.object({
myField: z.optional(z.string().min(4, "Please enter a valid value")),
});

This passes if I used a value like: "good", but if I've got an empty value then it fails.


How do I correctly implement a constraint using zod schemas to make an optional value with a minimum constraint if the value is not empty?


Is it possible to do this without using regex or a regex solution the only way?


More From » typescript

 Answers
70

In your case, you consider "" to be the same as undefined (i.e.: when the string is empty, it's like there's no string at all).


It's implementable in Zod this way:


import { z } from "zod";
import { strict as assert } from "node:assert";

// `myString` is a string that can be either optional (undefined or missing),
// empty, or min 4
const myString = z
.union([z.string().length(0), z.string().min(4)])
.optional()
.transform(e => e === "" ? undefined : e);

const schema = z.object({ test: myString });

assert( schema.parse({}).test === undefined ); // missing string
assert( schema.parse({ test: undefined }).test === undefined ); // string is undefined
assert( schema.parse({ test: "" }).test === undefined ); // string is empty
assert( schema.parse({ test: "1234" }).test === "1234" ); // string is min 4

// these successfully fail
assert( schema.safeParse({ test: "123" }).success !== true );
assert( schema.safeParse({ test: 3.14 }).success !== true );

[#50014] Monday, April 18, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
markusdamienn

Total Points: 167
Total Questions: 119
Total Answers: 93

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;