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

@bpframework/validation

v0.0.2

Published

Some typescript decorators, like spring-validation and so on

Downloads

4

Readme

Some typescript decorators, like spring-validation and so on.

Setup

npm i @bpframework/validation

set config in tsconfig.json

"experimentalDecorators": true,
"emitDecoratorMetadata": true,

Example

import {NotNull, Type} from '@bpframework/validation';

class BeanA {
  @NotNull
  @Type.Boolean
  a: boolean = null;
}

// will throw a exception: 
//  - febs.exception('message', febs.exception.PARAM, __filename, __line, __column);
let obj = new BeanA();

// will throw a exception
obj.a = null;
obj.a = undefined;

!!!WARNING: The following code does not trigger a validity check

class BeanA {
  @NotNull
  a: boolean;
}

// No exception is thrown.
let obj = new BeanA();

// Will throw a exception (trigger by settter)
obj.a = null;

All validate decorators have a corresponding array validation method, e.g. @Max.List use to validator array

like java spring validation

Null value validator

| 名称 | 作用 | 例子 | | --------------------------------- | ------------------------------------------------------------------------------------------------ | ------------------------------------- | | @Null | must be null or undefined. | @Null({message: "must is null"}) | | @NotNull | cannot be null or undefined. | @NotNull({message: "cannot be null"}) | | @NotBlank | must contain a non-null char trim(str).length>0 | @NotBlank | | @NotEmpty | cannot be null or undefined, and the length is greater then 0 (o.size() > 0 或 o.length > 0) | @NotEmpty | | @Size({max:number, min:number}) | check the length of a string or array (o.size() 或 o.length) |   |

Boolean validator

| 名称 | 作用 | 例子 | | -------------- | ------------- | ---------------------------------------- | | @AssertFalse | must be false | @AssertFalse({message: "must be false"}) | | @AssertTrue | muse be true | @AssertTrue({message: "muse be true"}) |

Number validator

| 名称 | 作用 | 例子 | | ------------------------------------------------------------ | ------------------------------------------------------------------- | ------------------------------------------- | | @DecimalMax({value:number ¦ string}) | Number value must be less than or equal to the specified value | @DecimalMax({value:1000, message: "error"}) | | @DecimalMin({value:number ¦ string}) | Number value must be greater than or equal to the specified value | @DecimalMin({value:0, message: "error"}) | | @Max({value:number ¦ string}) | Integer value must be less than or equal to the specified value. | @Max({value:1000, message: "error"}) | | @Min({value:number ¦ string}) | Integer value must be greater than or equal to the specified value. | @Min({value:1000, message: "error"}) | | [unsupported] ~~@Digits({integer:number, fraction:number})~~ | unsupported | @Digits({integer:5, fraction:1}) | | @Negative | Number value must be negative |   | | @NegativeOrZero | Number value must be negative or zero |   | | @Positive | Number value must be positive |   | | @PositiveOrZero | Number value must be positive or zero |   | | @Range({min:number=0, max:number}) | check the range of number value |   |

String validator

| 名称 | 作用 | 例子 | | --------------------------- | --------------------------------------------------------------- | ----------------------- | | @Email({regexp:RegExp}) | Whether the specified value is Email. You can specify a regular | @Email({regexp:/./}) | | @Pattern({regexp:RegExp}) | Whether the specified value matches regular | @Pattern({regexp:/./}) |

Time validator

| 名称 | 作用 | 例子 | | ------------------ | ---------------------------- | ------ | | @Future | Date value is the future time |   | | @FutureOrPresent | Date value is the future time or now |   | | @Past | Date value is the past time |   | | @PastOrPresent | Date value is the past time or now |   |

Enum validator

| 名称 | 作用 | | ------------------------------------ | ------------------------------- | | @Enum({allows: [v1, v2, v3, ...]}) | Verify the value is one of the allowed values. | | @Type.Enum({enumType: EnumName}) | Verify the value is enum type. |

@Type.Enum example:

import {Type} from '@bpframework/validation';

enum Enum1 {
  a = '2323',
  b = 'xxxx'
}

class BeanA {
  @Type.Enum({enumType: Enum1 })
  a: any = null;
}
let obj = new BeanA();

obj.a = Enum1.a;  // ok.
obj.a = Enum1.b;  // ok.
obj.a = 1;  // will throw a exception.

Type validator

| 名称 | 作用 | | --------------- | ------------------------ | | @Type.Boolean | Verify that the value is a Boolean. | | @Type.Number | Verify that the value is a Number. | | @Type.Integer | Verify that the value is a Integer. | | @Type.BigInt | Verify that the value is a Big-Integer. | | @Type.String | Verify that the value is a String. | | @Type.Date | Verify that the value is a Date or Date-string. | | @Type.Object | Verify that the value is a Object. | | @Type.Array | Verify that the value is a Array. |

Custom validator

| 名称 | 作用 | | ----------------- | ------------- | | @Type.Validator | Custom validator. |

// validator value == 1
@Type.Validator({
  checkCB(value:any) {
    if (value !== 1) return false;
  }
})
value: number;