|
1 | 1 | import ansis from 'ansis'; |
| 2 | +import { vol } from 'memfs'; |
| 3 | +import { readFile, stat } from 'node:fs/promises'; |
2 | 4 | import path from 'node:path'; |
3 | | -import z, { ZodError } from 'zod'; |
4 | | -import { SchemaValidationError, validate } from './validate.js'; |
| 5 | +import { ZodError, z } from 'zod'; |
| 6 | +import { SchemaValidationError, validate, validateAsync } from './validate.js'; |
5 | 7 |
|
6 | 8 | describe('validate', () => { |
7 | 9 | it('should return parsed data if valid', () => { |
@@ -37,6 +39,76 @@ describe('validate', () => { |
37 | 39 | ✖ Invalid ISO date |
38 | 40 | → at dateOfBirth`); |
39 | 41 | }); |
| 42 | + |
| 43 | + it('should throw if async schema provided (handled by validateAsync)', () => { |
| 44 | + const projectNameSchema = z |
| 45 | + .string() |
| 46 | + .optional() |
| 47 | + .transform( |
| 48 | + async name => |
| 49 | + name || JSON.parse(await readFile('package.json', 'utf8')).name, |
| 50 | + ) |
| 51 | + .meta({ title: 'ProjectName' }); |
| 52 | + |
| 53 | + expect(() => validate(projectNameSchema, undefined)).toThrow( |
| 54 | + 'Encountered Promise during synchronous parse. Use .parseAsync() instead.', |
| 55 | + ); |
| 56 | + }); |
| 57 | +}); |
| 58 | + |
| 59 | +describe('validateAsync', () => { |
| 60 | + it('should parse schema with async transform', async () => { |
| 61 | + vol.fromJSON({ 'package.json': '{ "name": "core" }' }, '/test'); |
| 62 | + const projectNameSchema = z |
| 63 | + .string() |
| 64 | + .optional() |
| 65 | + .transform( |
| 66 | + async name => |
| 67 | + name || JSON.parse(await readFile('package.json', 'utf8')).name, |
| 68 | + ) |
| 69 | + .meta({ title: 'ProjectName' }); |
| 70 | + |
| 71 | + await expect(validateAsync(projectNameSchema, undefined)).resolves.toBe( |
| 72 | + 'core', |
| 73 | + ); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should parse schema with async refinement', async () => { |
| 77 | + vol.fromJSON({ 'package.json': '{}' }, '/test'); |
| 78 | + const filePathSchema = z |
| 79 | + .string() |
| 80 | + .refine( |
| 81 | + file => |
| 82 | + stat(file) |
| 83 | + .then(stats => stats.isFile()) |
| 84 | + .catch(() => false), |
| 85 | + { error: 'File does not exist' }, |
| 86 | + ) |
| 87 | + .transform(file => path.resolve(process.cwd(), file)) |
| 88 | + .meta({ title: 'FilePath' }); |
| 89 | + |
| 90 | + await expect(validateAsync(filePathSchema, 'package.json')).resolves.toBe( |
| 91 | + path.join(process.cwd(), 'package.json'), |
| 92 | + ); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should reject with formatted error if async schema is invalid', async () => { |
| 96 | + vol.fromJSON({}, '/test'); |
| 97 | + const filePathSchema = z |
| 98 | + .string() |
| 99 | + .refine( |
| 100 | + file => |
| 101 | + stat(file) |
| 102 | + .then(stats => stats.isFile()) |
| 103 | + .catch(() => false), |
| 104 | + { error: 'File does not exist' }, |
| 105 | + ) |
| 106 | + .meta({ title: 'FilePath' }); |
| 107 | + |
| 108 | + await expect(validateAsync(filePathSchema, 'package.json')).rejects.toThrow( |
| 109 | + `Invalid ${ansis.bold('FilePath')}\n✖ File does not exist`, |
| 110 | + ); |
| 111 | + }); |
40 | 112 | }); |
41 | 113 |
|
42 | 114 | describe('SchemaValidationError', () => { |
|
0 commit comments