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

ts-matches-json-schema

v5.1.5

Published

We want to bring in some pattern matching into the typescript land. We want to be able to type guard a whole bunch of conditions and get the type back.

Downloads

2

Readme

ts-matches-json-schema

Coverage Status

The problem that we want solved is that we have generated out some JSON-Schema, but we would like the types in typescript. As well, we would like to validate these types are valid (throw/ case switch).

So this is going to be a subset of the potential json schema. If I can't get a typescript type from the schema, then I'm not going to make the validation. If you want to have full validation, but no typescript types, something like AJV.js is going to fit your bill better.

Example Usuage

Usage of converting from a schema to a parser/ ts-matcher

// Here we have brought in the test schema. This schema was cloned off
// https://json-schema.org/learn/getting-started-step-by-step.html
// except the reference.
// Notice: There is the `as const`. Currenlty one cannot import from csv with the const
const testSchema = {
  $schema: "http://json-schema.org/draft-07/schema#",
  $id: "http://example.com/product.schema.json",
  title: "Product",
  description: "A product from Acme's catalog",
  type: "object",
  properties: {
    productId: {
      description: "The unique identifier for a product",
      type: "integer",
    },
    productName: {
      description: "Name of the product",
      type: "string",
    },
    isProduct: {
      description: "Name of the product",
      type: "boolean",
    },
    price: {
      description: "The price of the product",
      type: "number",
      exclusiveMinimum: 0,
    },
    tags: {
      description: "Tags for the product",
      type: "array",
      items: {
        type: "string",
      },
      minItems: 1,
      uniqueItems: true,
    },
    extras: {
      description: "Tags for the product",
      type: "array",
      minItems: 1,
      uniqueItems: true,
    },
    dimensions: {
      type: "object",
      properties: {
        length: {
          type: "number",
        },
        width: {
          type: "number",
        },
        height: {
          type: "number",
        },
      },
      required: ["length", "width", "height"],
    },
  },
  required: ["productId", "productName", "price"],
} as const;

// Here we create our matcher
const matchTestSchema = asSchemaMatcher(testSchema);
// If we want to pull out the generated type from the matcher for typescript
type TestSchema = typeof matchTestSchema._TYPE;

// Here we are going to create a json to test the shape
const validShape = {
  productId: 0,
  price: 0.4,
  productName: "test",
  tags: ["5"],
  extras: ["string", 4],
  isProduct: false,
  dimensions: {
    length: 7.0,
    width: 12.0,
    height: 9.5,
  },
};

// So here, we are going to validate the shape.
// Note: Return type of unsafeCast is TestSchema here, so we don't manually
// need to tell typescript what the type is.
// Note: If this fails with unsafeCast, it will throw. There are other methods in the validator that will not throw.
const validShape: TestSchema = matchTestSchema.unsafeCast(validShape);

Convert a parser into a schema

import { literal, shape } from "https://deno.land/x/[email protected]/mod.ts";
const matcher = shape({ a: literal(5), b: literal("5").name("Is_string_5") }, ["b"]);
const schema = toSchema(matcher);
assertEquals(schema, {
  allOf: [
    {
      type: "object",
      properties: {
        b: { $ref: "#/definitions/Is_string_5" },
      },
      required: [],
    },
    {
      type: "object",
      properties: {
        a: {
          type: "number",
          enum: [5],
        },
      },
      required: ["a"],
    },
  ],
  definitions: { Is_string_5: { type: "string", enum: ["5"] } },
});

Features

  • [x] Convert parser into a schema
  • [x] Simple Type Validation
  • [x] Enum Type Validation
  • [x] allOf/ anyOf type
  • [x] Type allowed to be array of types
  • [x] References to other schemas
  • [ ] Limit Validations (min/ max)