npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@schemasjs/validator

v2.0.0

Published

An opinionated type validator API indpendent from schema validators like Zod, Valibot, etc

Downloads

50

Readme

@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 = '[email protected]'
    
    console.log(EmailSchema.is(myEmail) ? 'Valid email' : 'Invalid email') // 'Valid email'
    
    const parsedEmail = EmailSchema.parse(myEmail) // '[email protected]'
    
    const parsed = EmailSchema.safeParse(myEmail)
    
    console.log(parsed.success ? parsed.data : parsed.errors) // '[email protected]'

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