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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@riiixch/validate-input

v1.0.0

Published

Lightweight TypeScript validation library

Readme

🇺🇸 Read in English (English Version)


🛡️ @riiixch/validate-input

npm version License TypeScript

ไลบรารี Validation สำหรับ TypeScript ที่เบาและปลอดภัย > ตรวจสอบข้อมูลง่ายๆ พร้อมระบบสร้าง Type อัตโนมัติ ไม่ต้องเขียน Interface ซ้ำซ้อน!


🌟 จุดเด่น (Features)

  • 🚀 Type Inference Magic: สร้าง TypeScript Type จาก Schema ให้ทันที (ลดงานเขียนโค้ดซ้ำซ้อน)
  • 🧹 Auto Sanitization: ตัดช่องว่าง (trim), แปลงตัวพิมพ์ (toLowerCase), หรือตั้งค่า Default ให้เอง
  • 🌍 Multi-language (i18n): รองรับภาษาอังกฤษ (Default) และ ภาษาไทย 🇹🇭 ในตัว
  • 🧩 Custom Logic: เขียนฟังก์ชันตรวจสอบเงื่อนไขซับซ้อนได้เอง
  • 📦 Lightweight: ขนาดเล็ก เบา ไม่หนักโปรเจกต์

📦 การติดตั้ง (Installation)

npm install @riiixch/validate-input

🚀 เริ่มต้นใช้งาน (Quick Start)

import { defineSchema, validateInput } from '@riiixch/validate-input';

// 1. สร้าง Schema (ใช้ defineSchema เพื่อให้ได้ Type Inference)
const userSchema = defineSchema({
  username: { 
    type: 'string', 
    required: true, 
    minLength: 3, 
    trim: true // ตัดช่องว่างให้อัตโนมัติ
  },
  age: { 
    type: 'integer', 
    min: 18,
    defaultValue: 18 // ถ้าไม่ส่งมา จะใช้ค่า 18
  },
  email: {
    type: 'email',
    required: true
  }
});

// 2. ข้อมูล Input (เช่น จาก req.body)
const input = { 
  username: "  riiixch  ", 
  email: "[email protected]" 
};

// 3. ตรวจสอบข้อมูล
const result = validateInput(input, userSchema);

if (!result.success) {
  // ❌ กรณีไม่ผ่าน
  console.error(result.errorMsg); 
  // Output: "username is required" (หรือภาษาไทยตามที่ตั้งค่า)
} else {
  // ✅ กรณีผ่าน (TypeScript จะรู้ Type ของ data ทันที!)
  const { username, age, email } = result.data!;
  
  console.log(username); // "riiixch" (ถูก trim แล้ว)
  console.log(age);      // 18 (ค่า default)
}

🇹🇭 การใช้งานภาษาไทย (Localization)

คุณสามารถเปลี่ยนภาษาของ Error Message ได้ง่ายๆ 2 วิธี:

วิธีที่ 1: ตั้งค่า Global (แนะนำสำหรับทั้งโปรเจกต์)

ใส่โค้ดนี้ที่ไฟล์หลักของโปรเจกต์ (เช่น app.ts หรือ index.ts)

import { setLocale } from '@riiixch/validate-input';

// เปลี่ยนเป็นภาษาไทย
setLocale('th'); 

วิธีที่ 2: ตั้งค่าเฉพาะจุด (Per-request)

validateInput(data, schema, { lang: 'th' });

🛠️ คู่มือการกำหนด Schema (Schema Reference)

🔹 ประเภทข้อมูลที่รองรับ (Supported Types)

| Type | คำอธิบาย | Options พิเศษ | | :--- | :--- | :--- | | string | ข้อความ | minLength, maxLength, pattern, trim, toLowerCase, toUpperCase | | number | ตัวเลข (ทศนิยมได้) | min, max | | integer | จำนวนเต็มเท่านั้น | min, max | | boolean | ค่า true/false | - | | email | อีเมล | - | | password | เช็คความปลอดภัยรหัสผ่าน | minLength | | url | ลิงก์ URL | - | | datetime | วันที่ (Date/String) | minDate, maxDate | | enum | ค่าที่กำหนดไว้ | enumValues: ['A', 'B'] | | array | อาเรย์พื้นฐาน | arrayItemType, minLength | | object | ออบเจกต์ซ้อน | schema | | arrayOf | อาเรย์ของออบเจกต์ | arraySchema |

🔹 การเขียน Custom Validation (เขียนกฎเอง)

หากกฎพื้นฐานไม่เพียงพอ คุณสามารถเขียนฟังก์ชันเช็คเองได้

const schema = defineSchema({
  coupon: {
    type: 'string',
    required: true,
    // รับค่า value เข้ามา -> return true ถ้าผ่าน, หรือ return ข้อความ Error
    custom: (val) => {
      if (!val.startsWith('PROMO_')) {
        return 'รหัสคูปองต้องขึ้นต้นด้วย PROMO_';
      }
      return true;
    }
  }
});

📄 License

MIT © [RIIIXCH]