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

@lancercomet/yunomix

v1.5.3

Published

Yunomix is a validation toolkit which is designed for TypeScript users in AOP form.

Downloads

51

Readme

Yunomix

MyWebLibs npm version

Yunomix is a validation toolkit which is designed for TypeScript users in AOP form.

Features

  • Set rules by using @Decorator.
  • Works with Vuetify and Lancet.
  • For TypeScript users.

Quick start

// Please make sure "reflect-metadata" is imported at your project entry.
import 'reflect-metadata'
import {
  Required, IsString, IsEnglish, IsNumber, IsHexColor,
  getValidatorRules
} from '@lancercomet/yunomix'

class User {
  @Required()
  @IsString(1, 10, {
    invalidType: 'Name length must within 1-10.'
  })
  @IsEnglish()
  name: string = ''

  @IsNumber('Age must be a number.')
  age: number = 0

  @IsString(5)
  addr: string = ''

  @IsHexColor()
  color: string = '#4fc315'
}

const rules = getValidatorRules(User)
const userInput = new User()

In Vuetify:

<v-text-field v-model="userInput.name" :rules="rules.name" />
<v-text-field v-model.number="userInput.age" :rules="rules.age" />
<v-text-field v-model="userInput.addr" :rules="rules.age" />
<v-text-field v-model="userInput.color" :rules="rules.color" />

In Lancet:

<lct-textfield v-model="userInput.name" :rules="rules.name" />
<lct-textfield v-model.number="userInput.age" :rules="rules.age" />
<lct-textfield v-model="userInput.addr" :rules="rules.age" />
<lct-textfield v-model="userInput.color" :rules="rules.color" />

Without any UI framework:

// Import "validate".
import { validate } from '@lancercomet/yunomix'

class User {
  ...
}

const rules = getValidatorRules(User)
const userInput = new User()

// A true or a string will be returned.
// A true means it passed validation.
// A string means it didn't. This string is used as its error message.
const isNameValid = validate(userInput.name, rules.name)  // true | string

Validator list

  • IsArray
  • IsBoolean
  • IsChinese
  • IsEmail
  • IsEnglish
  • IsHexColor
  • IsHttpUrl
  • IsNumber
  • IsString
  • MatchValue
  • NumRange
  • Required
  • CustomRule

Custom Rule

You can use CustomRule to create a customized validator:

// It accepts multiple validating functions.
@CustomRule(
  v => typeof v === 'string' || 'Value must be a string',
  v => v.includes('John') || 'Value must contain a "John".'
)

Example:

class User {
  // "v" is something that user gives.
  @CustomRule(v => v === 'Kayne' || 'You must be Kayne!')
  name: string = ''
}

const input = new User()
const rules = getValidatorRules(User)

<Vtext-field v-model={input.name} rules={rules.user}>  // user.name can only be 'Kayne'.

CustomRule accepts one / several function(s) like (value: unknown) => true | string.

The returned value:

  • true: Indicates that the value that user provides is valid.
  • a string: Indicates that the value that user provides is invalid and this returned string will be used as the warning message.

Error message

Yunomix accepts both a string and a function as its error message:

class User {
  @Required('It should not be empty.')
  @IsString(() => {
    return window.navigator.language.includes('zh')
      ? '此项必须是一个字符串.'
      : 'This field should be a string.'
  })
  something: unknown = undefined
}

Both are OK. Function is very useful for i18n.

Example

Please check the example folder.

Something you might to know

Yunomix depends on the very feature emitDecoratorMetadata which was introduced in TypeScript, so keep in mind:

  • You have to install and import reflect-metadata manually.
  • Not available in pure JavaScript enviroment. You have to use it with TypeScript.
  • Not available for ESBuild because ESBuild doesn't emit decorator metadata.

License

Apache-2.0