@geeleed/validate-easy
v1.0.0
Published
Simple, async-friendly schema-based data validator for JavaScript and TypeScript
Maintainers
Readme
@geeleed/validate-easy
@geeleed/validate-easy คือไลบรารี JavaScript ขนาดเล็กที่ช่วยตรวจสอบความถูกต้องของข้อมูล (data validation) โดยใช้รูปแบบ schema ที่เข้าใจง่าย รองรับทั้ง synchronous และ asynchronous validation พร้อมรองรับ TypeScript
🚀 Features
- ✅ รองรับชนิดข้อมูล: string, number, boolean, array, object
- ✅ รองรับ object ซ้อน และ array
- ✅ รองรับ custom validation แบบ sync และ async
- ✅ เขียนง่าย ใช้งานสะดวก ไม่ต้องติดตั้ง dependency อื่น
- ✅ มี Type Definitions สำหรับ TypeScript
📦 การติดตั้ง
npm install @geeleed/validate-easy🔰 วิธีใช้งานเบื้องต้น
const { validateData } = require('@geeleed/validate-easy');
const schema = {
username: {
type: 'string',
required: true,
minLength: 3,
maxLength: 15
},
age: {
type: 'number',
required: true,
custom: (value) => value >= 18 || 'ต้องมีอายุอย่างน้อย 18 ปี'
},
email: {
type: 'string',
pattern: /^[^\s@]+@[^\s@]+\.[^\s@]+$/
}
};
const data = {
username: 'jo',
age: 16,
email: 'invalid-email'
};
(async () => {
const result = await validateData(schema, data);
console.log(result);
})();🧾 ผลลัพธ์ที่ได้
{
"valid": false,
"errors": {
"username": "Minimum length is 3",
"age": "ต้องมีอายุอย่างน้อย 18 ปี",
"email": "Invalid format"
}
}🔍 ตัวอย่างการใช้งานในกรณีต่าง ๆ
✅ Object ซ้อน (Nested Object)
const schema = {
address: {
type: 'object',
required: true,
properties: {
city: { type: 'string', required: true },
zip: { type: 'string', pattern: /^\d{5}$/ }
}
}
};
const data = {
address: {
city: '',
zip: 'abc'
}
};✅ ผลลัพธ์:
{
"valid": false,
"errors": {
"address.city": "This field is required",
"address.zip": "Invalid format"
}
}✅ Array + Schema สำหรับแต่ละรายการ
const schema = {
tags: {
type: 'array',
required: true,
items: {
type: 'string',
minLength: 2
}
}
};
const data = {
tags: ['ok', 2, 'a']
};✅ ผลลัพธ์:
{
"valid": false,
"errors": {
"tags[1].item": "Expected type string but got number",
"tags[2].item": "Minimum length is 2"
}
}✅ Custom Validation แบบ Async
async function isEmailTaken(email) {
const takenEmails = ['[email protected]'];
return takenEmails.includes(email);
}
const schema = {
email: {
type: 'string',
required: true,
custom: async (value) => {
const exists = await isEmailTaken(value);
return !exists || 'อีเมลนี้ถูกใช้งานแล้ว';
}
}
};
const data = {
email: '[email protected]'
};✅ ผลลัพธ์:
{
"valid": false,
"errors": {
"email": "อีเมลนี้ถูกใช้งานแล้ว"
}
}📘 รายการกฎที่ใช้ได้ใน schema
| กฎ | ประเภท | คำอธิบาย |
| ------------ | ------------------------------------------------------------------ | --------------------------- |
| type | 'string', 'number', 'boolean', 'array', 'object' | ชนิดข้อมูลที่รับได้ |
| required | boolean | กำหนดว่าต้องกรอกหรือไม่ |
| minLength | number | ความยาวขั้นต่ำของ string |
| maxLength | number | ความยาวสูงสุดของ string |
| pattern | RegExp | ตรวจสอบ string ด้วย pattern |
| items | SchemaRule | กำหนด schema ของ array item |
| properties | object | กำหนด schema ของ object |
| custom | (value, data) => boolean \| string \| Promise<boolean \| string> | ฟังก์ชันตรวจสอบแบบกำหนดเอง |
🧑💻 TypeScript Support
ใช้งานใน TypeScript ได้ทันที พร้อม auto-complete และ type checking:
import { validateData } from '@geeleed/validate-easy';
const schema = {
age: {
type: 'number',
required: true,
custom: (value) => value > 0 || 'อายุต้องมากกว่า 0'
}
};
const result = await validateData(schema, { age: -1 });
if (!result.valid) {
console.log(result.errors.age); // Type-safe
}