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

tsc-parsers

v0.0.4

Published

Validate JSON typescript objects at run-time

Downloads

2

Readme

Tsc-Parsers

What

An easy, intuitive parsing library to validate custom types at run-time.

Why

Typescript types, while amazing, are only during compile time and are erased during run-time. To combat this, I made a simple toy library that is surprisingly extensible and can validate types at runtime.

Examples include validating a string enum:

enum STRING_ENUM {
    'FIRST',
    'SECOND',
    'THIRD',
    'FOURTH',
}
const STRING_VALIDATOR = new StringEnumValidator(STRING_ENUM)

describe('StringEnumValidator', () => {

    it('correctly identifies valid string', () => {
        expect(STRING_VALIDATOR.validate('FIRST')).to.be.true
        expect(STRING_VALIDATOR.validate('SECOND')).to.be.true
        expect(STRING_VALIDATOR.validate('THIRD')).to.be.true
        expect(STRING_VALIDATOR.validate('FOURTH')).to.be.true
    })

    it('correctly identifies invalid strings', () => {
        // invalid strings
        expect(STRING_VALIDATOR.validate('omg')).to.be.false
        expect(STRING_VALIDATOR.validate('hi')).to.be.false
    })
})

Or an integer array:

describe('IntArrayValidator', () => {

    it('correctly identifies an int array', () => {
        expect(IntArrayValidator.validate([])).to.be.true
        expect(IntArrayValidator.validate([1.0, 2.0])).to.be.true
        expect(IntArrayValidator.validate([1, 2, 3, 4])).to.be.true
    })

    it('correctly identifies non- int arrays', () => {
        // strings
        expect(IntArrayValidator.validate('hello')).to.be.false

        // booleans
        expect(IntArrayValidator.validate(true)).to.be.false

        // numbers
        expect(IntArrayValidator.validate(0)).to.be.false

        // arrays
        expect(IntArrayValidator.validate(['example string', 'another one', true])).to.be.false
        expect(IntArrayValidator.validate(['example string', 6.0, true])).to.be.false
        expect(IntArrayValidator.validate([null, 6.0, true])).to.be.false
        expect(IntArrayValidator.validate([1, 0, 1.1])).to.be.false
        expect(IntArrayValidator.validate([1.0, 2.5, -4.89])).to.be.false
    })
})

And perhaps most importantly of all, custom objects:

const OBJECT_VALIDATOR = new ObjectValidator(
    // These are required fields
    {
        code: IntValidator,
        lat: FloatValidator,
        lon: FloatValidator,
        data: StringArrayValidator,
    }, {
        // Note that these are optional fields that are supported
        emergency: BooleanValidator,
    })

describe('ObjectValidator', () => {
    it('correctly identifies our valid defined model', () => {
        expect(OBJECT_VALIDATOR.validate({
            code: 200,
            lat: 0.0,
            lon: -58.23,
            data: [],
        })).to.be.true
        expect(OBJECT_VALIDATOR.validate({
            code: 200,
            lat: 0.0,
            lon: -58.23,
            data: ['hi', 'how are you'],
        })).to.be.true
        expect(OBJECT_VALIDATOR.validate({
            code: 200,
            lat: 0.0,
            lon: -58.23,
            data: ['hi', 'how are you'],
            emergency: true,
        })).to.be.true
        expect(OBJECT_VALIDATOR.validate({
            code: 200,
            lat: 0.0,
            lon: -58.23,
            data: ['hi', 'how are you'],
            emergency: false,
        })).to.be.true
    })
})

One advantage in the way that this is implemented is that all of these various Validator classes and objects are inherited from a common BaseValidator class, so the type hierarchy is extensible and generic enough to support things like using this in a decorator or creating custom ones simply by extending the base abstract class.

Documentation

This contains the full set of parsers, many of which are extensible, along with a brief description of what they do.

Arrays

BaseArrayValidator

The base array validator takes another validator and verifies that the input is an array and that each element in the array passes the test of the given validator.

BaseNonEmptyArrayValidator

Extends the BaseArrayValidator and in addition tests that the array is not empty

There are also non-empty array validator versions of all of the arrays you see below.

FloatArrayValidator

Extends the BaseArrayValidator and ensures that each element in the array is a number.

IntArrayValidator

Extends the BaseArrayValidator and ensures that each element in the array is an integer.

StringArrayValidator

Extends the BaseArrayValidator and ensures that each element in the array is a string.

Basic Types

BooleanValidator

Ensures the given input is a boolean.

DateValidator

Checks that the input is a Javascript Date object.

FloatValidator

Checks that the input is a number.

IntValidator

Checks that the input is an integer.

StringValidator

Checks that the input is a string.

SpecificStringValidator

Checks that the input is a string and matches the exact string given to the constructor.

Enums

Enums are very useful when trying to enforce discriminating type unions at runtime.

FloatEnumValidator

Checks that the input is one of the float values in the given enum.

IntEnumValidator

Checks that the input is one of the integer values in the given enum.

StringEnumValidator

Checks that the input is one of the string values in the given enum.

Objects

ObjectValidator

Probably the coolest of the lot - it takes in an object where the keys are field names and the values are validators. It also supports optional fields in a second constructor parameter of the same object schema (default initialized to an empty object).

Other

Want to make a custom validator? BaseValidator is the base class from which all others are extended. Feel free to implement your own!

Want to have runtime type safety but can't migrate the entire codebase all at once? Stub in NoOpValidator, which allows everything, and provides you the freedom to slowly adopt strict runtime enforcement.

Contribute

Want to help? Feel free to file a PR at ts-parse!