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

data-annotations

v1.4.1

Published

Some common object property limiters and customizable limiter injection are provided

Downloads

1

Readme

data-annotations

Some common object property limiters and customizable limiter injection are provided

中文文档

English

Built in three common limiters

  1. Required Restrictions applied when required fields
  2. Range It can put your numbers in a certain range, like 2-10
  3. RegularExpression It can match regular in string type properties

Installation

npm install data-annotations --save

Usage

Use built-in limiter

The built-in limiter is particularly convenient to use, you only need to simply add a modifier to the place you need to limit to call it up, like this

    export class Test{
        @Required               <-----
        public TestStr:string;
    }

Warning! If you use a modified limiter (like the one above), you need to call DataAnnotations.LimiterInit(obj) to initialize after the object is constructed to make the limiter effective

    export class Test{
        constructor(){
            DataAnnotations.LimiterInit(this);    <-----
        }
        @Required               
        public TestStr:string;
    }

When we need to check the limiter, we simply call a DataAnnotations.IsValid(obj), like this

    function foo(){
        const obj = new Test();
        const result = DataAnnotations.IsValid(obj);
        console.log(result.Success)
    }

Custom limiter

You can use the built-in DataAnnotations.DefineLimiter() to dynamically set a custom limiter to the properties of any object, even anonymous objects. if there is an error, it returns error content, by return Null

    const obj = { test:'123' }

    function foo(){
        DataAnnotations.DefineLimiter("MyLimiter",obj, "test", (arg) => {
            if (arg == "123") {
                return "you can't input '123'";
            }
            return null;
        })
    }

The built-in limiter provides a Factory for dynamic injection. You can use this method to dynamically inject the built-in interceptor or modify the error message


Reflection error message

You can set your ErrorMsg to bind properties to some forms to quickly get feedback information, for example

    export class Test{
        @Required("you need input Phone or Email")  <-----
        public TestStr:string;
    }

The error message can also be replaced when the outer layer is instantiated

    function foo(){
        const obj = new Test();
        RequiredFactory("Email will do")(obj,"TestStr");  <------
    }

Error listener

We sometimes even want to let the error prompt appear by itself instead of manually trigger

    function foo(){
        const obj = new Test();
        RequiredFactory("Email will do",(e)=>{
            //....Do something

        })(obj,"TestStr");
    }

Change listener

You can use the built-in DataAnnotations.SetChangeListener() to set a change listener to the properties of any object. Of course, anonymous is also possible, but the premise is that the target property needs at least one limiter, no matter how you set it

    const obj = { test:'123' }

    function foo(){
        DataAnnotations.SetChangeListener(obj,'test',(s)=>{
                console.log(s);
        })
    }