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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@demgel/validation

v0.0.1-beta.3

Published

Object validation with decorators

Readme

Demgel Validation Decorators

What is this?

There are times when you want to validate an object and assure that all properties are valid values.

Example

import {ValidationModel, Model, isNumber, isString} from "@demgel/validation";

@Model
export class SomeModel extends ValidationModel {
    @required()
    @isNumber()
    aNumber: number;

    @required()
    @isString()
    aString: string;
}

To check that a model is valid just use isValid().

if (someModel.isValid()) {
    // Do something with the data...
} else {
    // report Error
    this.errors.forEach((error, message) => {
        console.log(`${error}: ${message}`);
    };
}

If there is an error the model will contain a property called errors. errors is a Map<string, string>.

Custom Validators

You can write your own validators by using the generateValidator function.

returning null equals no error, returning a string will set the validator to error.

export declare function generateValidator(name: string, validator: any): (target: Object, propertyKey: string) => void;
export var someValidator = (options) => generateValidator("someValidator", (value) => {
    // Check the value to see if it matches
    if (valueisnotgood) {
        return "Value is not right";
    }
    return null;
}

export var anotherValidator = generateValidator("anotherValidator", (value) => {
    // Check the value to see if it is correct
    return null;
}

The first example, someValidator, would be used like this:

@Model()
export class SomeClass extends ValidatableModel {
    @someValidator("somepassedinvalue")
    property: string;
}

The second example, anotherValidator, would be used like this:

@Model()
export class SomeClass extends ValidatableModel {
    @anotherValidator
    property: string;
}

Parsing Object/JSON into/from Models

TO JSON

You should (and probably best) use the obj.toJSON() function from a ValidationModel to convert a Model to JSON. There are some underlying functions and values that should not be serialized, such as error.

Also there is a decorator that you can use excludeJSON that will let you mark properties that should be excluded from the JSON output.

@Model()
export class JSONClass extends ValidatableModel {
    @excludeJSON()
    property: string;
    aprop: number;
}

let cla = new JSONClass();
cla.property = 'something';
cla.aprop = 42;

cla.toJSON();

This will output:

{"aprop": 42}

notice the excluded property of property is not included.

FROM JSON/Object

@Model()
export class ClassWithProperties extends ValidatableModel {
    @required()
    prop: string;
    another: number;
    array: SomeType[];
}

Call your api and get this JSON string back.

{
    "prop": "a string",
    "another": 42,
    "array": ["value", "anothervalue"]
}
const jsonString = (api call returned value);

let classWithProperties = new ClassWithProperties();
classWithProperties.fromJSON(jsonString);
// classWithProperties is not populated with values from jsonString

// If you needed to parse the jsonString first
let obj = JSON.parse(jsonString);
classWithProperties.fromObject(obj);
// classWithProperties is now populated with values from obj