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

tvalidatorr

v0.1.1

Published

Validates JSO(N) inputs against Typescript Types

Downloads

10

Readme

tvalidatorr

Typescript JSO(N) input validator against Typescript Types

Motivation

Ability to validate various JSO(N) user/3rd patry inputs against type definitions.

Workings
  • errors will be thrown in the order of detection
  • read source files that contain types are cached in order to be reused
  • if no errors are thrown then the input is valid and conforms with the given type
Rules
  • optional properties are only validated against the type and not the presence
  • additional properties that are not defined in the source types are considered a violation of input validity

Installing

npm install --save tvalidatorr

Usage

Access to /src

Given type

export interface User {
    id: number;

    active: boolean

    address: Address;

    phone?: Array<string>;

    tasks: Array<Task>;
}

interface Address {
    street: string;

    county: string;

    flatNumber?: number;
}

interface Task {
    name: string;

    priority: number;

    createdAt?: Date;
}
Types can either be pre-loaded
// initialise by pre-loading types
await Validator.initialise({
    files: ['./lib/types/User.ts'],
    callerBaseDir: __dirname
});

const fakeUserInput = {
    id: 101,
    active: true
};

try {
    Validator.validateByName(fakeUserInput, 'User');
} catch (err) {
    console.error(err.message);
    // Input is missing property *address* for type User
}

const moreCompleteFakeUserInput = {
    id: 101,
    active: true,
    address: {
        street: 'Some Street 101'
    }
}

try {
    Validator.validateByName(moreCompleteFakeUserInput, 'User');
} catch (err) {
    console.error(err.message);
    // Input is missing property *address.county* for type User
}
Or loaded and used on the spot
const userWithInvalidTaskInput = {
    id: 101,
    active: true,
    address: {
        street: 'Some Street 101',
        county: 'County near you'
    },
    tasks: [
        {
            name: 'House Duties',
            priority: 'Should be a number!'
        }
    ]
}

try {
    await Validator.validateAsTypeSource(userWithInvalidTaskInput, {
        files: ['./lib/types/User.ts'],
        callerBaseDir: __dirname,
        typeName: 'User'
    });
} catch (err) {
    console.error(err.message);
    //  Input has a User type mismatch at path: *tasks[0].priority* - number type expected
}

Runtime without access to /src type files

Using tparserr to generate type description file

tparserr can be installed as dev and attached to build

npm install --save-dev tparserr

with e.g. npm script attached to the build

"build:types": "tparserr generate -f=./src/lib/types/User.ts -o=./types.json"

once types are described in said json file, they can be used and validated

await Validator.initialiseByTypeDescription('./types.json'); // relative type paths are resolved against process.cwd()

const fakeUserInput = {
    id: 101,
    active: true
};

try {
    Validator.validateByName(fakeUserInput, 'User');
} catch (err) {
    console.error(err.message);
    // Input is missing property *address* for type User
}

const moreCompleteFakeUserInput = {
    id: 101,
    active: true,
    address: {
        street: 'Some Street 101'
    }
}

try {
    Validator.validateByName(moreCompleteFakeUserInput, 'User');
} catch (err) {
    console.error(err.message);
    // Input is missing property *address.county* for type User
}

const userWithInvalidTaskInput = {
    id: 101,
    active: true,
    address: {
        street: 'Some Street 101',
        county: 'County near you'
    },
    tasks: [
        {
            name: 'House Duties',
            priority: 'Should be a number!'
        }
    ]
}

try {
    Validator.validateByName(userWithInvalidTaskInput, 'User');
} catch (err) {
       console.error(err.message);
       //  Input has a User type mismatch at path: *tasks[0].priority* - number type expected
}

License

This library is licensed under the Apache 2.0 License