JSPM

@minsize/rtl

1.0.2
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 50
  • Score
    100M100P100Q76941F
  • License MIT

Lightweight, fast and type-safe runtime type validation for TypeScript

Package Exports

  • @minsize/rtl
  • @minsize/rtl/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@minsize/rtl) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

RTI

Минималистичная библиотека для валидации данных в TypeScript с выводом типов на этапе разработки.

Установка

npm install @minsize/rti

Быстрый пример

import * as rti from "@minsize/rti"

const userSchema = rti.object({
  id: rti.pipe(rti.number(), rti.positive()),
  name: rti.pipe(rti.string(), rti.trim(), rti.min(2), rti.max(50)),
  email: rti.pipe(rti.string(), rti.email()),
  createdAt: rti.date(),
  bio: rti.defaultValue(rti.string(), ""),
  tags: rti.array(rti.string()),
})

const result = userSchema({
  id: 1,
  name: "  Alex  ",
  email: "alex@example.com",
  createdAt: "2024-01-01T00:00:00.000Z",
  tags: ["ts", "validation"],
})

if (result.ok) {
  console.log(result.value.name) // "Alex"
  console.log(result.value.createdAt instanceof Date) // true
} else {
  console.error(result.error)
}

Что умеет RTI

  • Примитивы: string, number, bigint, boolean, date, any, unknown
  • Структуры: object, array, tuple, record, union
  • Модификаторы: optional, null, nullable, nullish, defaultValue, fallback
  • Композиция: pipe, transform
  • Ограничения: min, max, check, instance
  • Числовые проверки: integer, float, finite, safeInteger, positive, negative и другие
  • Строковые проверки: email, url, uuid, regex, includes, startsWith, endsWith, trim и другие
  • Вывод типов через InferInput и InferOutput

Документация

Полная документация для GitBook лежит в docs/README.md.

Если хочешь открыть её как книгу, используй SUMMARY.md.