|
1 | | -import { z } from "zod"; |
2 | | -import { |
3 | | - deepClone, |
4 | | - getErrorMessage, |
5 | | - isObject, |
6 | | - sanitize, |
7 | | -} from "../../src/ts/utils/misc"; |
| 1 | +import { deepClone, getErrorMessage, isObject } from "../../src/ts/utils/misc"; |
8 | 2 | import { |
9 | 3 | getLanguageDisplayString, |
10 | 4 | removeLanguageSize, |
@@ -233,127 +227,4 @@ describe("misc.ts", () => { |
233 | 227 | }); |
234 | 228 | }); |
235 | 229 | }); |
236 | | - describe("sanitize function", () => { |
237 | | - const schema = z |
238 | | - .object({ |
239 | | - name: z.string(), |
240 | | - age: z.number().positive(), |
241 | | - tags: z.array(z.string()), |
242 | | - enumArray: z.array(z.enum(["one", "two"])).min(2), |
243 | | - }) |
244 | | - .partial() |
245 | | - .strip(); |
246 | | - |
247 | | - it("should return the same object if it is valid", () => { |
248 | | - const obj = { name: "Alice", age: 30, tags: ["developer", "coder"] }; |
249 | | - expect(sanitize(schema, obj)).toEqual(obj); |
250 | | - }); |
251 | | - |
252 | | - it("should remove properties with invalid values", () => { |
253 | | - const obj = { name: "Alice", age: -5, tags: ["developer", "coder"] }; |
254 | | - expect(sanitize(schema, obj)).toEqual({ |
255 | | - name: "Alice", |
256 | | - tags: ["developer", "coder"], |
257 | | - age: undefined, |
258 | | - }); |
259 | | - }); |
260 | | - |
261 | | - it("should remove invalid array elements", () => { |
262 | | - const obj = { |
263 | | - name: "Alice", |
264 | | - age: 30, |
265 | | - tags: ["developer", 123, "coder"] as any, |
266 | | - }; |
267 | | - expect(sanitize(schema, obj)).toEqual({ |
268 | | - name: "Alice", |
269 | | - age: 30, |
270 | | - tags: ["developer", "coder"], |
271 | | - }); |
272 | | - }); |
273 | | - |
274 | | - it("should remove invalid array elements with min size", () => { |
275 | | - const schema = z |
276 | | - .object({ |
277 | | - name: z.string(), |
278 | | - tags: z.array(z.enum(["coder", "developer"])).min(2), |
279 | | - }) |
280 | | - .partial(); |
281 | | - const obj = { |
282 | | - name: "Alice", |
283 | | - tags: ["developer", "unknown"] as any, |
284 | | - }; |
285 | | - expect(sanitize(schema, obj)).toEqual({ |
286 | | - name: "Alice", |
287 | | - }); |
288 | | - }); |
289 | | - |
290 | | - it("should remove entire property if all array elements are invalid", () => { |
291 | | - const obj = { name: "Alice", age: 30, tags: [123, 456] as any }; |
292 | | - const sanitized = sanitize(schema, obj); |
293 | | - expect(sanitized).toEqual({ |
294 | | - name: "Alice", |
295 | | - age: 30, |
296 | | - }); |
297 | | - expect(sanitized).not.toHaveProperty("tags"); |
298 | | - }); |
299 | | - |
300 | | - it("should remove object properties if they are invalid", () => { |
301 | | - const obj = { name: 123 as any, age: 30, tags: ["developer", "coder"] }; |
302 | | - const sanitized = sanitize(schema, obj); |
303 | | - expect(sanitized).toEqual({ |
304 | | - age: 30, |
305 | | - tags: ["developer", "coder"], |
306 | | - }); |
307 | | - expect(sanitized).not.toHaveProperty("name"); |
308 | | - }); |
309 | | - |
310 | | - it("should remove nested objects if not valid", () => { |
311 | | - //GIVEN |
312 | | - const schema = z |
313 | | - .object({ |
314 | | - name: z.string(), |
315 | | - info: z.object({ age: z.number() }).partial(), |
316 | | - }) |
317 | | - .partial(); |
318 | | - |
319 | | - const obj = { |
320 | | - name: "Alice", |
321 | | - info: { age: "42" as any }, |
322 | | - }; |
323 | | - //WHEN / THEN |
324 | | - expect(sanitize(schema, obj)).toEqual({ |
325 | | - name: "Alice", |
326 | | - }); |
327 | | - }); |
328 | | - |
329 | | - it("should strip extra keys", () => { |
330 | | - const obj = { |
331 | | - name: "bob", |
332 | | - age: 30, |
333 | | - tags: ["developer", "coder"], |
334 | | - powerLevel: 9001, |
335 | | - } as any; |
336 | | - const stripped = sanitize(schema.strip(), obj); |
337 | | - expect(stripped).not.toHaveProperty("powerLevel"); |
338 | | - }); |
339 | | - it("should strip extra keys on error", () => { |
340 | | - const obj = { |
341 | | - name: "bob", |
342 | | - age: 30, |
343 | | - powerLevel: 9001, |
344 | | - } as any; |
345 | | - const stripped = sanitize(schema.strip(), obj); |
346 | | - expect(stripped).not.toHaveProperty("powerLevel"); |
347 | | - }); |
348 | | - it("should provide a readable error message", () => { |
349 | | - const obj = { |
350 | | - arrayOneTwo: ["one", "nonexistent"], |
351 | | - } as any; |
352 | | - expect(() => { |
353 | | - sanitize(schema.required().strip(), obj); |
354 | | - }).toThrowError( |
355 | | - "unable to sanitize: name: Required, age: Required, tags: Required, enumArray: Required" |
356 | | - ); |
357 | | - }); |
358 | | - }); |
359 | 230 | }); |
0 commit comments