New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

bun-plugin-typia

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bun-plugin-typia

Adds support for [typia](https://github.com/samchon/typia), a transformer library that features super-fast runtime validators.

latest
npmnpm
Version
0.1.0
Version published
Maintainers
1
Created
Source

bun-plugin-typia

Adds support for typia, a transformer library that features super-fast runtime validators.

Installation

bun add bun-plugin-typia -d

Plugin usage

This plugin can be used to run typia validators at runtime with Bun without needing to create a template and generate the files:

  • Create a preload script to register the plugin via Bun.plugin()
// typiaPlugin.ts
import typiaPlugin from "bun-plugin-typia";

Bun.plugin(typiaPlugin());
  • Register the plugin in bunfig.toml
# bunfig.toml
preload = ["./typiaPlugin.ts"]

# for using in tests
[test]
preload = ["./typiaPlugin.ts"]
  • Use typia validators in your scripts and test files:
// index.ts
import typia, { tags } from "typia";
 
const res: typia.IValidation<IMember> = typia.validate<IMember>({
  id: 5, // wrong, must be string (uuid)
  age: 20.75, // wrong, not integer
  email: "danial@hambergerpls.com",
});
 
if (!res.success) console.log(res.errors);
// [
//   {
//     path: "$input.id",
//     expected: "(string & Format<\"uuid\">)",
//     value: 5,
//   }, {
//     path: "$input.age",
//     expected: "number & Type<\"uint32\">",
//     value: 20.75,
//   }
// ]
 
interface IMember {
  id: string & tags.Format<"uuid">;
  email: string & tags.Format<"email">;
  age: number &
    tags.Type<"uint32"> &
    tags.ExclusiveMinimum<19> &
    tags.Maximum<100>;
}

// test.ts
test("should be able to use validate function", async () => {
    const res: typia.IValidation<IMember> = typia.validate<IMember>({
        id: 5, // wrong, must be string (uuid)
        age: 20.75, // wrong, not integer
        email: "danial@hambergerpls.com",
      });
    
    
    expect(res.success).toEqual(false);
    expect(res.errors).toEqual([
        {
          path: "$input.id",
          expected: "(string & Format<\"uuid\">)",
          value: 5,
        }, {
          path: "$input.age",
          expected: "number & Type<\"uint32\">",
          value: 20.75,
        }
      ]);
  });

$ bun run index.ts

Contributing

$ bun install # project setup
$ bun test # run tests

FAQs

Package last updated on 13 Mar 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts