This package has been deprecated

Author message:

use https://github.com/azu/create-validator-ts

create-ts-validator
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

create-ts-validator

Create JSON Schema validator from TypeScript.

Motivation

  • Generate JSON Schema Validator functions from TypeScript code
  • Make TypeScript a single source of truth

Structure

.
└── src/
    ├── hello/
    │   ├── api-types.ts
    │   ├── api-types.validator.ts <- Generated
    │   └── index.ts
    └── status/
        ├── api-types.ts
        ├── api-types.validator.ts <- Generated
        └── index.ts

Install

Install with npm:

npm install create-ts-validator

Usage

Usage
  $ create-ts-validator [file|glob*]

Options
  --watch               [Boolean] If set the flag, start watch mode
  --check               [Boolean] If set the flag, start test mode
  --tsconfigFilePath    [Path:String] path to tsconfig.json
  --cwd                 [Path:String] current working directory
  --generatorScript     [Path:String] A JavaScript file path that customize validator code generator
  --verbose             [Boolean] If set the flag, show progressing logs

Examples
  $ create-ts-validator "src/**/api-types.ts"
  # custom tsconfig.json
  $ create-ts-validator "src/**/api-types.ts" --tsconfigFilePath ./tsconfig.app.json
  # custom validator code
  $ create-ts-validator "src/**/api-types.ts" --generatorScript ./custom.js

Example

You can generate validator code via following command

$ create-ts-validator "src/**/api-types.ts"

Default validator require ajv, and you need to install ajv into your project.

$ npm install ajv

Structure:

.
├ tsconfig.json
└── src/
    └─── api/
        ├──  api-types.ts
        └─── api-types.validator.ts <- Generated

api-types.ts:

// Example api-types
// GET /api
export type GetAPIRequestQuery = {
    id: string;
};
export type GetAPIResponseBody = {
    ok: boolean;
};

api-types.validator.ts (generated):

// @ts-nocheck
// eslint-disable
// This file is generated by create-ts-validator
import Ajv from 'ajv';
import * as apiTypes from './api-types';

const SCHEMA = {
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "GetAPIRequestQuery": {
            "type": "object",
            "properties": {
                "id": {
                    "type": "string"
                }
            },
            "required": [
                "id"
            ],
            "additionalProperties": false
        },
        "GetAPIResponseBody": {
            "type": "object",
            "properties": {
                "ok": {
                    "type": "boolean"
                }
            },
            "required": [
                "ok"
            ],
            "additionalProperties": false
        }
    }
};
const ajv = new Ajv({ removeAdditional: true }).addSchema(SCHEMA, "SCHEMA");
export function validateGetAPIRequestQuery(payload: unknown): apiTypes.GetAPIRequestQuery {
  if (!isGetAPIRequestQuery(payload)) {
   const error = new Error('invalid payload: GetAPIRequestQuery');
    error.name = "ValidatorError";
    throw error;
  }
  return payload;
}

export function isGetAPIRequestQuery(payload: unknown): payload is apiTypes.GetAPIRequestQuery {
  /** Schema is defined in {@link SCHEMA.definitions.GetAPIRequestQuery } **/
  const ajvValidate = ajv.compile({ "$ref": "SCHEMA#/definitions/GetAPIRequestQuery" });
  return ajvValidate(payload);
}

export function validateGetAPIResponseBody(payload: unknown): apiTypes.GetAPIResponseBody {
  if (!isGetAPIResponseBody(payload)) {
   const error = new Error('invalid payload: GetAPIResponseBody');
    error.name = "ValidatorError";
    throw error;
  }
  return payload;
}

export function isGetAPIResponseBody(payload: unknown): payload is apiTypes.GetAPIResponseBody {
  /** Schema is defined in {@link SCHEMA.definitions.GetAPIResponseBody } **/
  const ajvValidate = ajv.compile({ "$ref": "SCHEMA#/definitions/GetAPIResponseBody" });
  return ajvValidate(payload);
}

Custom Validator

You can create custom validator using --generatorScript flag.

$ create-ts-validator "src/**/api-types.ts" --generatorScript ./custom.js

custom.js

"use strict";
const path = require("path");
const generator = ({ apiFilePath, apiFileCode, schema }) => {
    const apiFileName = path.basename(apiFilePath, ".ts");
    const isExportedTypeInApiTypes = (apiName) => {
        return (apiFileCode.includes(`export type ${apiName} =`) ||
            apiFileCode.includes(`export interface ${apiName} {`));
    };
    const banner = `// @ts-nocheck
// eslint-disable
// This file is generated by create-ts-validator
import Ajv from 'ajv';
import logger from 'logger';
import * as apiTypes from './${apiFileName}';
`;
    // define SCHEMA to top, and we can refer it as "SCHEMA".
    // Note: { "$ref": "SCHEMA#/definitions/${apiName}" }
    const schemaDefinition = `const SCHEMA = ${JSON.stringify(schema, null, 4)};
const ajv = new Ajv({ removeAdditional: true }).addSchema(SCHEMA, "SCHEMA");`;
    const code = Object.entries(schema.definitions || {})
        .filter(([apiName]) => {
        return isExportedTypeInApiTypes(apiName);
    })
        .map(([apiName, _schema]) => {
        return `export function validate${apiName}(payload: unknown): apiTypes.${apiName} {
  if (!is${apiName}(payload)) {
   const error = new Error('invalid payload: ${apiName}');
    error.name = "ValidatorError";
    throw error;
  }
  return payload;
}

export function is${apiName}(payload: unknown): payload is apiTypes.${apiName} {
  /** Schema is defined in {@link SCHEMA.definitions.${apiName} } **/
  const ajvValidate = ajv.compile({ "$ref": "SCHEMA#/definitions/${apiName}" });
  return ajvValidate(payload);
}`;
    })
        .join("\n\n");
    return `${banner}
${schemaDefinition}
${code}
`;
};
exports.generator = generator;

Ignore generated Code

If you used Prettier, you should add *.validator.ts to .prettierignore

*.validator.ts

Changelog

See Releases page.

Running tests

Install devDependencies and Run npm test:

npm test

Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, please create an issue.

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

Author

License

MIT © azu

Prior art

Differences:

Package Sidebar

Install

npm i create-ts-validator

Weekly Downloads

1

Version

0.1.2

License

MIT

Unpacked Size

60.4 kB

Total Files

32

Last publish

Collaborators

  • azu