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

typescript-json-validator

v2.4.2

Published

Automatically generate a validator using JSON Schema and AJV for any TypeScript type.

Downloads

9,462

Readme

TypeScript JSON Validator

Automatically generate a validator using JSON Schema and AJV for any TypeScript type.

Usage

Define a type in src/Example.ts, e.g.:

export default interface ExampleType {
  value: string;
  /**
   * @TJS-format email
   */
  email?: string;
  /**
   * @default 42
   */
  answer: number;
}

To generate a validator, run:

npx typescript-json-validator src/Example.ts ExampleType

This will generate src/Example.validator.ts, which you can use:

import {readFileSync} from 'fs';
import validate from './Example.validator.ts';

const value: unknown = JSON.parse(readFileSync(process.argv[2], 'utf8'));

// this will through a clear error if `value` is not of the
// correct type. It will also fill in any default values
const validatedValue = validate(value);

console.log(validatedValue.value);

Note that types will be validated automatically, but you can also use annotations to add extra runtime checks, such as e-mail formatting. For annotations see: https://github.com/YousefED/typescript-json-schema#annotations

CLI Docs

Usage: typescript-json-schema <path-to-typescript-file> <type>

Options:
  --help                Show help                                      [boolean]
  --version             Show version number                            [boolean]
  --refs                Create shared ref definitions. [boolean] [default: true]
  --aliasRefs           Create shared ref definitions for the type aliases.
                                                      [boolean] [default: false]
  --topRef              Create a top-level ref definition.
                                                      [boolean] [default: false]
  --titles              Creates titles in the output schema.
                                                      [boolean] [default: false]
  --defaultProps        Create default properties definitions.
                                                       [boolean] [default: true]
  --noExtraProps        Disable additional properties in objects by default.
                                                      [boolean] [default: false]
  --propOrder           Create property order definitions.
                                                      [boolean] [default: false]
  --typeOfKeyword       Use typeOf keyword (https://goo.gl/DC6sni) for
                        functions.                    [boolean] [default: false]
  --required            Create required array for non-optional properties.
                                                       [boolean] [default: true]
  --strictNullChecks    Make values non-nullable by default.
                                                       [boolean] [default: true]
  --ignoreErrors        Generate even if the program has errors.
                                                      [boolean] [default: false]
  --validationKeywords  Provide additional validation keywords to include.
                                                           [array] [default: []]
  --excludePrivate      Exclude private members from the schema.
                                                      [boolean] [default: false]
  --uniqueNames         Use unique names for type symbols.
                                                      [boolean] [default: false]
  --include             Further limit tsconfig to include only matching files.
                                                                         [array]
  --rejectDateType      Rejects Date fields in type definitions.
                                                      [boolean] [default: false]
  --id                  ID of schema.                     [string] [default: ""]
  --uniqueItems         Validate `uniqueItems` keyword [boolean] [default: true]
  --unicode             calculate correct length of strings with unicode pairs
                        (true by default). Pass false to use .length of strings
                        that is faster, but gives "incorrect" lengths of strings
                        with unicode pairs - each unicode pair is counted as two
                        characters.                    [boolean] [default: true]
  --nullable            support keyword "nullable" from Open API 3
                        specification.                 [boolean] [default: true]
  --format              formats validation mode ('fast' by default). Pass 'full'
                        for more correct and slow validation or false not to
                        validate formats at all. E.g., 25:00:00 and 2015/14/33
                        will be invalid time and date in 'full' mode but it will
                        be valid in 'fast' mode.
                                     [choices: "fast", "full"] [default: "fast"]
  --coerceTypes         Change data type of data to match type keyword. e.g.
                        parse numbers in strings      [boolean] [default: false]
  --collection          Process the file as a collection of types, instead of
                        one single type.              [boolean] [default: false]
  --useNamedExport      Type name is a named export, rather than the default
                        export of the file            [boolean] [default: false]
  -*                                                               [default: []]

License

MIT