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

obj-schema-validator

v1.1.11

Published

Lightweight and simple validator for objects and DTOs, designed for TypeScript and Node.js projects.

Downloads

58

Readme

Obj-schema-validator

Intalling

Using npm:

$ npm install obj-schema-validator

Once the package is installed, you can import the library using the import or require approach:

import { DtoSchema, Schema } from "obj-schema-validator";

Later you must define a schema for the use of a dto, this by declaring a class that extends the DtoSchema class, you must implement the schema attribute of type Schema.

class UserDto extends DtoSchema {
    schema: Schema = {
        name: {
            required: true,
            type: "string",
            options: {
                toTitleCase: true,
            }
        },
        age: {
            required: false,
            type: "number",
        },
        city: {
            required: true,
            type: "string",
            options: {
                includes:  ["CDMX", "Queretaro"]
            }
        }

    };

}

The schema attribute is a hashmap where each key is an object in which it is declared whether it is required, the type, and as optional values ​​its initial value and an options object.

schema: Schema = {
        name: {
            required: true,
            type: "string",
            options: {
                toTitleCase: true,
            }
        },
        email: {
            required: true,
            type: "string",
            options: {
                toLowerCase: true,
                isEmail: true,
            }
        },
        age: {
            required: false,
            type: "number",
            options: {
                minimunValue: 5
            }
        },
        city: {
            required: true,
            type: "string",
            options: {
                includes: ["Mexico", "Queretaro"] // indicate the list of allowed values 

            }
        },
    };

The options object has these optional values

type options = {
    toTitleCase: boolean, 
    // Converts the string to "Title Case", that is, capitalizes the first letter of each word and lowercases the rest.

    toUpperCase: boolean,
    // Converts the entire string to uppercase.

    toLowerCase: boolean,
    // Converts the entire string to lowercase
    
    isEmail: boolean,
    // Checks if the string is a valid email using a standard regular expression for validating emails

    includes: Array<any>,
    // Checks if the string is within the array of provided values.

    checkPattern: RegExp,
    // Check if the string matches the provided pattern (regular expression).
    
    isLength: number,
    // Checks if the length of the string is exactly equal to the provided number.
    
    minLength: number,
    // Checks if the length of the string is greater than or equal to the provided number.

    maxLength: number,
    // Checks if the length of the string is less than or equal to the provided number.

    minimunValue: number,
    // Checks if the numeric value of the string is greater than or equal to the provided number.
        
    maximunValue: number,
    // Checks if the numeric value of the string is less than or equal to the provided number.
}

You can use the create method of your dto to carry out the validations, this method returns an array with two positions, the first being the error and the second being the values ​​already validated.

const data = {
    name: "test lastname",
    age: "5",
    email: "[email protected]",
    password: "test",
    city: "CDMX",
}

const [error, result] = new UserDto().create(data);