/ESCSS-ESTest

Non-intrusive JavaScript validator

Primary LanguageJavaScriptOtherNOASSERTION

logo

Why ESCSS-ESTest?

Just for a guy who wants to survive in a massive, legacy JavaScript/TypeScript codebase.

Features

  • 💪 JavaScript version of TypeScript + Zod: Ditch any & complexity.
  • 💣 No vender lock-in.
  • 🐛 Find bug quickly.
  • ❤️‍🔥 DX first, DX first, DX first and security!
  • 🪶 3.65 kB (minified + gzipped), 0 dependencies.
  • ✅ Autocompletion support.
  • ⚡ (Optional) The definitive choice for high-performance.

benchmark

benchmark

source: ESTest-benchmark-repo

Installation

  npm add escss-estest

Table of Contents

Core Concept

  • ESTest: console.error --> decoupling / isESTestDisabled = true for high-performance
  • unSafeESTest: throw new Error
  • ESTestForLibrary: The default message is separated from ESTest & unSafeESTest

Core API

ESTest(input: unknown, type: string, message: string)

Validate Type (TypeScript Part)

import { ESTest } from "escss-estest";

function sum(a, b) {
  {
    // validate type
    ESTest(a, "number");
    ESTest(b, "number");
  }

  // do something
}

Validate Schema (Zod Part)

import { ESTest } from "escss-estest";

async function getApi() {
  const apiData = await fetch("https://jsonplaceholder.typicode.com/todos/1");
  const data = await apiData.json();

  // const data = {
  //   id: 1,
  //   name: 'Mike',
  //   info: {
  //     title: "developer",
  //     more: [
  //       {
  //         msg: 'Hello!',
  //       },
  //       {
  //         msg: 'Hi!',
  //       }
  //     ]
  //   },
  // }

  {
    // validate schema
    ESTest(data, "object", "schema mismatch").schema({
      id: "number",
      "name?": "string",
      info: {
        title: "string",
        more: [
          {
            msg: "string",
          },
        ],
      },
    });

    // validate detail
    ESTest(data.id, "number", "custom msg").min(0).max(50);
  }

  // do something
}

getApi();

unSafeESTest(input: unknown, type: string, message: string)

Usage is exactly the same as ESTest

import { unSafeESTest } from "escss-estest";
import express from "express";

const app = express();
const port = 3000;

app.use(express.json());

app.post("/demo", (req, res) => {
  try {
    const data = req.body;

    // const data = {
    //   id: 1,
    //   name: 'Mike',
    //   info: {
    //     title: "developer",
    //     more: [
    //       {
    //         msg: 'Hello!',
    //       },
    //       {
    //         msg: 'Hi!',
    //       }
    //     ]
    //   },
    // }

    {
      // validate schema
      ESTest(data, "object", "schema mismatch").schema({
        id: "number",
        "name?": "string",
        info: {
          title: "string",
          more: [
            {
              msg: "string",
            },
          ],
        },
      });

      // validate detail
      unSafeESTest(data.id, "number", "custom msg").min(0).max(50);
    }

    // do something

    res.json({ message: "ok" });
  } catch (err) {
    res.status(400).json({ message: err.message });
  }
});

app.listen(port, () => {
  console.log(`http://localhost:${port}`);
});

ESTestForLibrary(input: unknown, type: string, message: string)

Library's own default message

import { ESTestForLibrary } from "escss-estest";

function ESTest(
  input,
  type,
  message = "[LibraryName] default message for others to help debugging",
) {
  return ESTestForLibrary(input, type, message);
}

Helper API

globalThis.__ESCSS_ESTEST__.information

  • Show information
information

globalThis.__ESCSS_ESTEST__.message

  • Set default message for your project.
globalThis.__ESCSS_ESTEST__.message = "Please report this issue to ...";

globalThis.__ESCSS_ESTEST__.isESTestDisabled

  • true: Disable to get high-performance. (for production)
  • false: Show Bug detail.

Note: unSafeESTest will not be affected (for security reasons)

globalThis.__ESCSS_ESTEST__.isESTestDisabled = true;

function sum(a, b) {
  {
    ESTest(a, "number");
    ESTest(b, "number");
  }

  return a + b;
}

// same as
function sum(a, b) {
  return a + b;
}

globalThis.__ESCSS_ESTEST__.analysis

  • Show usage reports
analysis

Thanks