Thanks to visit codestin.com
Credit goes to Github.com

Skip to content

arthur-forks/azs

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

azs

azs allows you to add virtual methods to zod schemas, tying data types to functions. here's a small example for a user type.

A more advanced example can be found here.

import {z} from 'zod';
import azs from 'azs';

const userSchema = z.object({
	age: z.number(),
	name: z.string(),
	github: z.string().url(),
});

const schema = azs(userSchema, {
	// Basic example
	isAdult: user => {
		return user.age >= 18;
	},

	// Methods can take arguments, but the
	// first argument will always be the parsed
	// value. You also don't have to type
	// the first argument.
	is: (user, name: string) => {
		return user.name === name;
	},

	// Or, you can access `this` which will be
	// the parsed value
	getName() {
		return this.name;
	},
});

const user = schema.parse(someRandomJSONThatMightBeAUser);

user.isAdult();
user.getName();
user.is('Colin McDonnell');

About

🤓 Amplify Zod schemas with methods

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 78.7%
  • JavaScript 21.3%