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

gearworks-validation

v1.2.0

Published

A TypeScript wrapper around the Joi package, providing typed object validation for Gearworks apps.

Downloads

11

Readme

gearworks-validation

A TypeScript wrapper around the Joi package, providing typed object validation for Gearworks apps. Gearworks is the best way to get started with building Shopify applications!

Installing

You can install this package from NPM with the NPM CLI or with Yarn (recommended):

# With NPM
npm install gearworks-validation --save

# With Yarn
yarn add gearworks-validation

Importing

You can import the validators either one function at a time, or all at once, via require or TypeScript's import:

// Import all functions
import * as gwv from "gearworks-validation";

// Import just one function
import { strings } from "gearworks-validation";

// Import all functions via Node's require:
const gwv = require("gearworks-validation");

// Import just one function via Node's require:
const strings = require("gearworks-validation").strings;

Usage

Most of the functions are straightforward and self-explanatory. Where this package really shines is when using the functions that provide intellisense for TypeScript types: object, numbers, onlyStrings, onlyStringsOrEmpty, and array.

import * as gwv from "gearworks-validation";

type MyString = "hello" | "world";

type MyNumber = 1 | 2 | 3;

interface MyType {
    foo: string;
    bar: number;
    baz: boolean;
    bat: MyString[];
    bang: MyNumber[];
}

const schema = gwv.object<MyType>({
    // We now have intellisense on MyType property names.
    foo: gwv.string().required(),
    bar: gwv.number(),
    baz: gwv.boolean(),
    bat: gwv.onlyStrings<MyString>("hello", "world" /* And we have intellisense on these strings. */),
    bang: gwv.numbers<MyNumber>(1, 2, 3 /* And these numbers! */)
})

Why?

Joi is a great package, which I use liberally throughout Gearworks and my own custom Shopify apps, but it has one big pitfall that we can easily fall into when working with plain JS packages: there's no type intellisense for the objects that I'm validating!

I would have zero intellisense for validating an object with plain Joi; I'd have to open my interface definition in a side-by-side window to make sure I was validating all of the properties I needed to validate. Worse, though, was that I wouldn't have any warnings or compiler errors when I changed a property on my interfaces. I'd just have to cross my fingers and hope that I updated all occurrences.

That problem is now solved with gearworks-validation.