@schemasjs/validator
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

@SchemasJS/validator

version Bundle size minified build

If you love type validators like Zod or Valibot, probably you load a ton of logic inside schemas. If suddenly you wanted to swap from one type validator to another, the migration would be a pain. This package try to minimize this problem.

Validator is an opinionated API to work with your favourites schemas coming from Zod or Valibot. You can use your preferred schemas in and if you need to swap to another it won't be a problem.

At this moment it is only supported Zod and Valibot.

How to use Validator

  1. In your schemas file, import the proper Validator that you need and create your validator for each schema.

    // Zod
    import { z } from 'zod'
    import { ZodValidator } from '@schemasjs/validator'
    import { Uint8Schema as ZodUint8Schema, type Uint8 } from '@schemasjs/zod-numbers'
    
    const Uint8Schema = new ZodValidator<Uint8>()
    const ZodEmailSchema = z.string().email()
    type Email = z.infer<typeof ZodEmailSchema>
    const EmailSchema = ZodValidator<Email>(ZodEmailSchema)
    // Valibot
    import * as v from 'valibot'
    import { ValibotValidator } from '@schemasjs/validator'
    import { Uint8Schema as ValibotUint8Schema, type Uint8 } from '@schemasjs/valibot-numbers'
    
    const Uint8Schema = ValibotValidator<Uint8>()
    const ValibotEmailSchema = v.pipe(v.string(), v.email())
    type Email = v.InferInput<typeof ValibotEmailSchema>
    const EmailSchema = ValibotValidator<Email>(ValibotEmailSchema)
  2. Then use it in your code instead your schemas, and that's all.

    const myEmail = 'foo@bar.com'
    
    console.log(EmailSchema.is(myEmail) ? 'Valid email' : 'Invalid email') // 'Valid email'
    
    const parsedEmail = EmailSchema.parse(myEmail) // 'foo@bar.com'
    
    const parsed = EmailSchema.safeParse(myEmail)
    
    console.log(parsed.success ? parsed.data : parsed.errors) // 'foo@bar.com'

Validator API

is: (data: any) => boolean

parse: (data: any) => T

safeParse: (data: any) => SafeParse<T>

It is a tiny API based on Zod API + is Valibot function API.

safeParse return

interface SafeParseSuccess<T> {
  success: true
  value: T
}

interface SafeParseFailure {
  success: false
  errors: string[]
}

export type SafeParse<T> = SafeParseSuccess<T> | SafeParseFailure

Package Sidebar

Install

npm i @schemasjs/validator

Weekly Downloads

27

Version

2.0.0

License

MIT

Unpacked Size

10.9 kB

Total Files

6

Last publish

Collaborators

  • crisconru