Skip to content

Commit

Permalink
Merge pull request #5 from vbudovski/feature/map-validator
Browse files Browse the repository at this point in the history
feature: Map validator
  • Loading branch information
vbudovski authored Jul 20, 2024
2 parents d50c996 + 77c24d4 commit c9b97bc
Show file tree
Hide file tree
Showing 7 changed files with 498 additions and 3 deletions.
43 changes: 43 additions & 0 deletions paseri-docs/src/content/docs/reference/Collections/map.mdx
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);
```
47 changes: 47 additions & 0 deletions paseri-lib/bench/map/type.bench.ts
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);
});
1 change: 1 addition & 0 deletions paseri-lib/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export {
bigint,
boolean,
literal,
map,
never,
null_ as null,
number,
Expand Down
8 changes: 5 additions & 3 deletions paseri-lib/src/infer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ type Infer<SchemaType> = Simplify<
? InferMapped<SchemaType>
: SchemaType extends Set<Schema<infer OutputType>>
? Set<OutputType>
: SchemaType extends Schema<infer OutputType>
? OutputType
: never
: SchemaType extends Map<Schema<infer OutputKeyType>, Schema<infer OutputValueType>>
? Map<OutputKeyType, OutputValueType>
: SchemaType extends Schema<infer OutputType>
? OutputType
: never
>;

export type { Infer };
1 change: 1 addition & 0 deletions paseri-lib/src/schemas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { array } from './array.ts';
export { bigint } from './bigint.ts';
export { boolean } from './boolean.ts';
export { literal } from './literal.ts';
export { map } from './map.ts';
export { never } from './never.ts';
export { null_ } from './null.ts';
export { number } from './number.ts';
Expand Down
Loading

0 comments on commit c9b97bc

Please sign in to comment.