generated from StyleShit/create-typescript-package
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,81 @@ | ||
# Bodek | ||
|
||
A zod-like schema validator | ||
A zod-like schema validator, 100% type-safe. | ||
|
||
Made for fun and learning purposes. | ||
|
||
## Installation | ||
|
||
```bash | ||
npm i bodek | ||
``` | ||
|
||
## Usage | ||
|
||
The API is basically the same as [Zod](https://zod.dev/)'s, but only with the core functionality (again, learning purposes...) | ||
|
||
### `parse`: | ||
|
||
```typescript | ||
import { b } from 'bodek'; | ||
|
||
const schema = b.object({ | ||
name: b.string().min(3).max(10), | ||
age: b.number().min(18), | ||
}); | ||
|
||
// No errors | ||
schema.parse({ | ||
name: 'John', | ||
age: 30, | ||
}); | ||
|
||
// Throws an error | ||
schema.parse({ | ||
name: 'John', | ||
age: 10, | ||
}); | ||
``` | ||
|
||
### `safeParse`: | ||
|
||
```typescript | ||
import { b } from 'bodek'; | ||
|
||
const schema = b.object({ | ||
name: b.string().min(3).max(10), | ||
age: b.number().min(18), | ||
}); | ||
|
||
const { success, data, error } = schema.safeParse({ | ||
name: 'John', | ||
age: 30, | ||
}); | ||
|
||
if (success) { | ||
console.log(data); | ||
} else { | ||
console.error(error); | ||
} | ||
``` | ||
|
||
### `refine`: | ||
|
||
```typescript | ||
import { b } from 'bodek'; | ||
|
||
const schema = b.object({ | ||
name: b | ||
.string() | ||
.min(3) | ||
.max(10) | ||
.refine((name) => name.toLowerCase() !== 'john', 'Name cannot be John'), | ||
age: b.number().min(18), | ||
}); | ||
|
||
// Throws an error | ||
schema.parse({ | ||
name: 'John', | ||
age: 30, | ||
}); | ||
``` |
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