-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from vbudovski/feature/map-validator
feature: Map validator
- Loading branch information
Showing
7 changed files
with
498 additions
and
3 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
paseri-docs/src/content/docs/reference/Collections/map.mdx
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,43 @@ | ||
--- | ||
title: "Map" | ||
sidebar: | ||
order: 26 | ||
--- | ||
|
||
```typescript | ||
import * as p from '@vbudovski/paseri'; | ||
|
||
const schema = p.map(p.number(), p.string()); | ||
const data = new Map([[1, 'foo'], [2, 'bar'], [3, 'baz']]); | ||
|
||
const result = schema.safeParse(data); | ||
if (result.ok) { | ||
// result.value typed as `Map<number, string>`. | ||
} | ||
``` | ||
|
||
## Validators | ||
|
||
### `min` | ||
|
||
Consists of at least `size` elements. | ||
|
||
```typescript | ||
p.map(p.number(), p.string()).min(3); | ||
``` | ||
|
||
### `max` | ||
|
||
Consists of at most `size` elements. | ||
|
||
```typescript | ||
p.map(p.number(), p.string()).max(3); | ||
``` | ||
|
||
### `length` | ||
|
||
Consists of exactly `size` elements. | ||
|
||
```typescript | ||
p.map(p.number(), p.string()).size(3); | ||
``` |
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,47 @@ | ||
import { z } from 'zod'; | ||
import * as p from '../../src/index.ts'; | ||
|
||
const { bench } = Deno; | ||
|
||
const paseriSchema = p.map(p.number(), p.string()); | ||
const zodSchema = z.map(z.number(), z.string()); | ||
|
||
const dataValid = new Map<number, string>([ | ||
[1, 'foo'], | ||
[2, 'bar'], | ||
[3, 'baz'], | ||
[4, 'foo'], | ||
[5, 'bar'], | ||
[6, 'baz'], | ||
[7, 'foo'], | ||
[8, 'bar'], | ||
[9, 'baz'], | ||
[10, 'foo'], | ||
[11, 'bar'], | ||
[12, 'baz'], | ||
[13, 'foo'], | ||
[14, 'bar'], | ||
[15, 'baz'], | ||
[16, 'foo'], | ||
[17, 'bar'], | ||
[18, 'baz'], | ||
[19, 'foo'], | ||
[20, 'bar'], | ||
]); | ||
const dataInvalid = null; | ||
|
||
bench('Paseri', { group: 'Type valid' }, () => { | ||
paseriSchema.safeParse(dataValid); | ||
}); | ||
|
||
bench('Zod', { group: 'Type valid' }, () => { | ||
zodSchema.safeParse(dataValid); | ||
}); | ||
|
||
bench('Paseri', { group: 'Type invalid' }, () => { | ||
paseriSchema.safeParse(dataInvalid); | ||
}); | ||
|
||
bench('Zod', { group: 'Type invalid' }, () => { | ||
zodSchema.safeParse(dataInvalid); | ||
}); |
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 |
---|---|---|
|
@@ -3,6 +3,7 @@ export { | |
bigint, | ||
boolean, | ||
literal, | ||
map, | ||
never, | ||
null_ as null, | ||
number, | ||
|
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
Oops, something went wrong.