-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(add-zod): Replace .nonempty with .refine(...refinementNonEmpty)
z.array().nonempty() influences the type definitions, and causes issues with Array.prototype.map() not being aware of the underlying array's cardinality.
- Loading branch information
1 parent
df0174d
commit 1639884
Showing
7 changed files
with
84 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { z } from 'zod' | ||
|
||
import { refinementNonEmpty } from './zod-refinements' | ||
|
||
describe('refinementNonEmpty', () => { | ||
const schema = z.array(z.number()).refine(...refinementNonEmpty) | ||
|
||
it('works for arrays > 1', () => { | ||
expect(schema.safeParse([1])).toEqual({ data: [1], success: true }) | ||
}) | ||
|
||
it('rejects arrays without items', () => { | ||
const result = schema.safeParse([]) | ||
|
||
expect(result.success).toBe(false) | ||
|
||
// type hint | ||
if ('error' in result) | ||
expect(result.error.errors).toEqual([ | ||
{ code: 'custom', message: 'array may not be empty', path: [] }, | ||
]) | ||
else throw new Error('error.result doesn’t exist') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { z } from 'zod' | ||
|
||
/** | ||
* Similar to z.array().nonempty(), but doesn’t alter type definitions | ||
* | ||
* We previously had issues with `.nonempty()` causing arrays generated from | ||
* `Array.prototype.map` to be detected as possibly empty. | ||
* | ||
* @example | ||
* | ||
* z.array(z.number()).refine(...refinementNonEmpty) | ||
*/ | ||
export const refinementNonEmpty: Parameters< | ||
z.ZodArray<z.ZodAny, 'many'>['refine'] | ||
> = [ | ||
(array: unknown[]) => array.length > 0, | ||
{ message: 'array may not be empty' }, | ||
] |