|
| 1 | +import { expect, test } from "vitest"; |
| 2 | +import { z } from "../../../../index.js"; |
| 3 | +import es from "../../../locales/es.js"; |
| 4 | + |
| 5 | +test("Spanish locale - type name translations in too_small errors", () => { |
| 6 | + z.config(es()); |
| 7 | + |
| 8 | + // Test string type translation |
| 9 | + const stringSchema = z.string().min(5); |
| 10 | + const stringResult = stringSchema.safeParse("abc"); |
| 11 | + expect(stringResult.success).toBe(false); |
| 12 | + if (!stringResult.success) { |
| 13 | + expect(stringResult.error.issues[0].message).toBe( |
| 14 | + "Demasiado pequeño: se esperaba que texto tuviera >=5 caracteres" |
| 15 | + ); |
| 16 | + } |
| 17 | + |
| 18 | + // Test number type translation |
| 19 | + const numberSchema = z.number().min(10); |
| 20 | + const numberResult = numberSchema.safeParse(5); |
| 21 | + expect(numberResult.success).toBe(false); |
| 22 | + if (!numberResult.success) { |
| 23 | + expect(numberResult.error.issues[0].message).toBe("Demasiado pequeño: se esperaba que número fuera >=10"); |
| 24 | + } |
| 25 | + |
| 26 | + // Test array type translation |
| 27 | + const arraySchema = z.array(z.string()).min(3); |
| 28 | + const arrayResult = arraySchema.safeParse(["a", "b"]); |
| 29 | + expect(arrayResult.success).toBe(false); |
| 30 | + if (!arrayResult.success) { |
| 31 | + expect(arrayResult.error.issues[0].message).toBe( |
| 32 | + "Demasiado pequeño: se esperaba que arreglo tuviera >=3 elementos" |
| 33 | + ); |
| 34 | + } |
| 35 | + |
| 36 | + // Test set type translation |
| 37 | + const setSchema = z.set(z.string()).min(2); |
| 38 | + const setResult = setSchema.safeParse(new Set(["a"])); |
| 39 | + expect(setResult.success).toBe(false); |
| 40 | + if (!setResult.success) { |
| 41 | + expect(setResult.error.issues[0].message).toBe("Demasiado pequeño: se esperaba que conjunto tuviera >=2 elementos"); |
| 42 | + } |
| 43 | +}); |
| 44 | + |
| 45 | +test("Spanish locale - type name translations in too_big errors", () => { |
| 46 | + z.config(es()); |
| 47 | + |
| 48 | + // Test string type translation |
| 49 | + const stringSchema = z.string().max(3); |
| 50 | + const stringResult = stringSchema.safeParse("abcde"); |
| 51 | + expect(stringResult.success).toBe(false); |
| 52 | + if (!stringResult.success) { |
| 53 | + expect(stringResult.error.issues[0].message).toBe("Demasiado grande: se esperaba que texto tuviera <=3 caracteres"); |
| 54 | + } |
| 55 | + |
| 56 | + // Test number type translation |
| 57 | + const numberSchema = z.number().max(10); |
| 58 | + const numberResult = numberSchema.safeParse(15); |
| 59 | + expect(numberResult.success).toBe(false); |
| 60 | + if (!numberResult.success) { |
| 61 | + expect(numberResult.error.issues[0].message).toBe("Demasiado grande: se esperaba que número fuera <=10"); |
| 62 | + } |
| 63 | + |
| 64 | + // Test array type translation |
| 65 | + const arraySchema = z.array(z.string()).max(2); |
| 66 | + const arrayResult = arraySchema.safeParse(["a", "b", "c"]); |
| 67 | + expect(arrayResult.success).toBe(false); |
| 68 | + if (!arrayResult.success) { |
| 69 | + expect(arrayResult.error.issues[0].message).toBe("Demasiado grande: se esperaba que arreglo tuviera <=2 elementos"); |
| 70 | + } |
| 71 | +}); |
| 72 | + |
| 73 | +test("Spanish locale - type name translations in invalid_type errors", () => { |
| 74 | + z.config(es()); |
| 75 | + |
| 76 | + // Test string expected, number received |
| 77 | + const stringSchema = z.string(); |
| 78 | + const stringResult = stringSchema.safeParse(123); |
| 79 | + expect(stringResult.success).toBe(false); |
| 80 | + if (!stringResult.success) { |
| 81 | + expect(stringResult.error.issues[0].message).toBe("Entrada inválida: se esperaba texto, recibido número"); |
| 82 | + } |
| 83 | + |
| 84 | + // Test number expected, string received |
| 85 | + const numberSchema = z.number(); |
| 86 | + const numberResult = numberSchema.safeParse("abc"); |
| 87 | + expect(numberResult.success).toBe(false); |
| 88 | + if (!numberResult.success) { |
| 89 | + expect(numberResult.error.issues[0].message).toBe("Entrada inválida: se esperaba número, recibido texto"); |
| 90 | + } |
| 91 | + |
| 92 | + // Test boolean expected, null received |
| 93 | + const booleanSchema = z.boolean(); |
| 94 | + const booleanResult = booleanSchema.safeParse(null); |
| 95 | + expect(booleanResult.success).toBe(false); |
| 96 | + if (!booleanResult.success) { |
| 97 | + expect(booleanResult.error.issues[0].message).toBe("Entrada inválida: se esperaba booleano, recibido nulo"); |
| 98 | + } |
| 99 | + |
| 100 | + // Test array expected, object received |
| 101 | + const arraySchema = z.array(z.string()); |
| 102 | + const arrayResult = arraySchema.safeParse({}); |
| 103 | + expect(arrayResult.success).toBe(false); |
| 104 | + if (!arrayResult.success) { |
| 105 | + expect(arrayResult.error.issues[0].message).toBe("Entrada inválida: se esperaba arreglo, recibido objeto"); |
| 106 | + } |
| 107 | +}); |
| 108 | + |
| 109 | +test("Spanish locale - fallback for unknown type names", () => { |
| 110 | + z.config(es()); |
| 111 | + |
| 112 | + // Test with a type that's not in the TypeNames dictionary |
| 113 | + // This will test the fallback behavior |
| 114 | + const dateSchema = z.date().min(new Date("2025-01-01")); |
| 115 | + const dateResult = dateSchema.safeParse(new Date("2024-01-01")); |
| 116 | + expect(dateResult.success).toBe(false); |
| 117 | + if (!dateResult.success) { |
| 118 | + // Should use "fecha" since we included it in TypeNames |
| 119 | + expect(dateResult.error.issues[0].message).toContain("fecha"); |
| 120 | + } |
| 121 | +}); |
| 122 | + |
| 123 | +test("Spanish locale - other error cases", () => { |
| 124 | + z.config(es()); |
| 125 | + |
| 126 | + // Test invalid_element with tuple |
| 127 | + const tupleSchema = z.tuple([z.string(), z.number()]); |
| 128 | + const tupleResult = tupleSchema.safeParse(["abc", "not a number"]); |
| 129 | + expect(tupleResult.success).toBe(false); |
| 130 | + if (!tupleResult.success) { |
| 131 | + expect(tupleResult.error.issues[0].message).toContain("Entrada inválida"); |
| 132 | + } |
| 133 | + |
| 134 | + // Test invalid_value with enum |
| 135 | + const enumSchema = z.enum(["a", "b"]); |
| 136 | + const enumResult = enumSchema.safeParse("c"); |
| 137 | + expect(enumResult.success).toBe(false); |
| 138 | + if (!enumResult.success) { |
| 139 | + expect(enumResult.error.issues[0].message).toBe('Opción inválida: se esperaba una de "a"|"b"'); |
| 140 | + } |
| 141 | + |
| 142 | + // Test not_multiple_of |
| 143 | + const multipleSchema = z.number().multipleOf(3); |
| 144 | + const multipleResult = multipleSchema.safeParse(10); |
| 145 | + expect(multipleResult.success).toBe(false); |
| 146 | + if (!multipleResult.success) { |
| 147 | + expect(multipleResult.error.issues[0].message).toBe("Número inválido: debe ser múltiplo de 3"); |
| 148 | + } |
| 149 | + |
| 150 | + // Test unrecognized_keys |
| 151 | + const strictSchema = z.object({ a: z.string() }).strict(); |
| 152 | + const strictResult = strictSchema.safeParse({ a: "test", b: "extra" }); |
| 153 | + expect(strictResult.success).toBe(false); |
| 154 | + if (!strictResult.success) { |
| 155 | + expect(strictResult.error.issues[0].message).toBe('Llave desconocida: "b"'); |
| 156 | + } |
| 157 | + |
| 158 | + // Test invalid_union |
| 159 | + const unionSchema = z.union([z.string(), z.number()]); |
| 160 | + const unionResult = unionSchema.safeParse(true); |
| 161 | + expect(unionResult.success).toBe(false); |
| 162 | + if (!unionResult.success) { |
| 163 | + expect(unionResult.error.issues[0].message).toBe("Entrada inválida"); |
| 164 | + } |
| 165 | + |
| 166 | + // Test invalid_format with regex |
| 167 | + const regexSchema = z.string().regex(/^[a-z]+$/); |
| 168 | + const regexResult = regexSchema.safeParse("ABC123"); |
| 169 | + expect(regexResult.success).toBe(false); |
| 170 | + if (!regexResult.success) { |
| 171 | + expect(regexResult.error.issues[0].message).toBe("Cadena inválida: debe coincidir con el patrón /^[a-z]+$/"); |
| 172 | + } |
| 173 | + |
| 174 | + // Test invalid_format with startsWith |
| 175 | + const startsWithSchema = z.string().startsWith("hello"); |
| 176 | + const startsWithResult = startsWithSchema.safeParse("world"); |
| 177 | + expect(startsWithResult.success).toBe(false); |
| 178 | + if (!startsWithResult.success) { |
| 179 | + expect(startsWithResult.error.issues[0].message).toBe('Cadena inválida: debe comenzar con "hello"'); |
| 180 | + } |
| 181 | +}); |
0 commit comments