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

@antify/validate

v1.14.0

Published

Use one validation for client and server side.

Downloads

78

Readme

Antify Validate

Use one validation for client and server side.

Features

  • [x] Validation for client and server side
  • [x] Validation per field, custom group or all fields
  • [x] Validation for nested objects
  • [x] Fully typed
  • [x] Ships with predefined validation rules (See here)
  • [ ] Ships with predefined and translated error messages

Installation

pnpm i @antify/validate

Usage

import { validate } from '@antify/validate';

export type Car = {
  id: string | null
  model: string | null
  manufacturer: string | null
  type: string | null
  color: string | null
  engine: {
    type: string | null
    volumeInLiter: number | null
    powerPs: number | null
  }
};

export const validator = useValidator<Car>({
  id: {
    rules: [(val: any) => isTypeOfRule(val, Types.STRING)],
    groups: ['client']
  }
})

Client side (Vue example)


<script lang="ts" scoped>
  import {ref, reference} from 'vue';
  import {validator} from './validator';

  const validatorRef = reference(validator);
  const car = ref({
    id: '1',
    model: null,
    manufacturer: null,
    type: null,
    color: null,
    engine: {
      type: null,
      volumeInLiter: null,
      powerPs: null
    }
  });
  
  function submit() {
    validatorRef.validate(car.value);

    if (validatorRef.hasErrors()) {
      return;
    }

    // submit
  }
</script>

<template>
  <form @submit.prevent="submit">
    <div v-if="validatorRef.hasErrors()">
      There are some errors to fix: <br>
      
      <div style="white-space: pre">{{ validatorRef.getErrorsAsString() }}</div>
    </div>
    
    <div>
      <label>{{ validatorRef.fieldMap.model.readableName }}</label>
      <input v-model="car.model" @blur="validatorRef.fieldMap.model.validate()"/>
      <span v-if="validatorRef.fieldMap.model.hasErrors()">{{ validatorRef.fieldMap.model.getErrors() }}</span>
    </div>
    
    <button>Submit</button>
  </form>
</template>

Server side

import {validator, type Car} from './validator';

const car: Car = {
  id: '1',
  model: 'A4',
  manufacturer: 'Audi',
  type: 'Sedan',
  color: 'Black',
  engine: {
    type: 'Gasoline',
    volumeInLiter: 2.0,
    powerPs: 150
  }
};

const validatedData = validator.validate(car);

if (validator.hasErrors()) {
  throw new Error(validator.getErrorsAsString());
}

// Save data

Transformer

You can use a transformer to transform the data before validation. A reusable list of transformers you can find here.

Example

import { validate, useValidator } from '@antify/validate';

export const queryValidator = useValidator({
  page: {
    transform: stringToNumberTransformer,
    rules: [(val: any) => isTypeOfRule(val, Types.NUMBER)]
  }
})

const data = queryValidator.validate({ page: '1' });

// data.page === 1
// Rule will be successful

Write your own transformer

import {validate, useValidator} from '@antify/validate';

export const queryValidator = useValidator({
  isAdmin: {
    transform: (val) => {
      if (typeof val === 'boolean' || val === 'true') {
        return !!val
      }

      if (val === 'false') {
        return false
      }

      return val
    },
    rules: [(val: any) => isTypeOfRule(val, Types.BOOLEAN)]
  }
})

const data = queryValidator.validate({isAdmin: 'true'});

// data.isAdmin === true
// Rule will be successful

Development

  • Run pnpm dev to generate type stubs.
  • Use pnpm dev to start tests in watch mode for development.