Portable and simple schemas for property validation.
- Easy and understandable schemas
- Contains ready regex patterns
- Portable schemas as a JSON file
- Schemas can be declared for TypeScript
- Schemas can be converted to JSON Schema. JSON Schema is OpenAPI compatible
- Error messages are ready to be understood but can be edited if desired
You can install it as follows.
# NPM
npm add teyit
# PNPM
pnpm add teyit
# Yarn
yarn add teyit
# Bun
bun add teyit
# Deno
deno add teyitBriefly as follows.
teyit
│
├── new Teyit(options?)
│ │
│ ├── validate(schema, properties)
│ ├── declare(schema, name)
│ └── convertToJSONSchema(schema)
│
├── Patterns
│ │
│ ├── Domain
│ ├── Email
│ ├── HTTP
│ ├── PhoneNumber
│ ├── URI
│ └── Username
│
├── type AnyObject
├── type InferSchema
├── type JSONSchema
├── type Schema
├── type TeyitOptions
└── type ValidationErrorBriefly as follows.
import { Teyit, Patterns } from 'teyit';new Teyit(options?)
Teyit schema builder.
Parameter Type Default Description options?TeyitOptions TeyitOptionsDefault Constructor's options. Example:
const teyit = new Teyit();
Teyit.validate(schema, properties)
Validate the properties with your Teyit schema.
Parameter Type Default Description schemaSchema Teyit schema. propertiesAnyObject Properties to be validate. returns Promise<InferSchema<Schema>>
Example:
const schema = { display_name: { type: 'string', max: 32, nullable: false, required: true }, username: { type: 'string', min: 3, max: 16, pattern: Patterns.Username, nullable: false, required: true }, email: { type: 'string', pattern: Patterns.Email, lowercase: true, nullable: false, required: true }, permissions: [ { type: 'string', enum: ['*'], nullable: false, required: true }, { type: 'array', items: { type: 'string', enum: ['read', 'write'], nullable: false, required: true }, nullable: false, required: true } ] } as const satisfies Schema; const properties = { display_name: 'Fırat', username: 'fir4tozden', email: 'fir4tozden@gmail.com', permissions: '*' }; let fields; try { fields = await teyit.validate(schema, properties); /* { display_name: "Fırat", username: "fir4tozden", email: "fir4tozden@gmail.com", permissions: "*" } */ } catch (error) { const errors = (error as ValidationError).errors; console.log(errors[0]); /* { message: "Field email must match the required pattern", parts: { path: "email" }, code: "field-email-must-match-the-required-pattern", } */ } console.log(fields.display_name); // "Fırat"
Teyit.declare(schema, name)
Declare your Teyit schema for TypeScript.
Parameter Type Default Description schemaSchema Teyit schema. nameString Declaration name. Example:
import type { User } from './generated/teyit/types/User'; await teyit.declare(schema, 'User'); let fields; try { fields = (await teyit.validate(schema, properties)) as User; /* interface User { display_name: string; username: string; email: string; permissions: "*" | ("read" | "write")[]; } */ } catch (error) { // ... }
Teyit.convertToJSONSchema(schema)
Convert your Teyit schema into JSON Schema.
Parameter Type Default Description schemaSchema Teyit schema. returns JSONSchema
Example:
teyit.convertToJSONSchema(schema); /* { additionalProperties: false, type: 'object', required: ['display_name', 'username', 'email', 'permissions'], properties: { display_name: { maxLength: 32, trim: true, type: 'string' }, username: { minLength: 3, maxLength: 16, pattern: '^(?=.*[a-zA-Z])[a-zA-Z0-9][a-zA-Z0-9_]*$', trim: true, type: 'string' }, email: { pattern: '^[a-zA-Z0-9._-]+@([a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,}$', trim: true, lowercase: true, uppercase: true, type: 'string' }, permissions: { anyOf: [ { enum: ['*'], trim: true, type: 'string' }, { type: 'array', items: { enum: ['read', 'write'], trim: true, type: 'string' } } ] } } } */
| Pattern | Description | Examples |
|---|---|---|
| Domain | Domains. | "google.com" ✅"www.google.com" ✅"https://google.com" ❌ |
| Emails. | "fir4tozden@gmail.com" ✅"fir4tozden+2@gmail.com" ❌ |
|
| HTTP | HTTP only links. | "https://google.com" ✅"http://google.com" ✅"google.com" ❌ |
| PhoneNumber | Country code and phone number. | "0090-555555555" ✅"90-5555555555" ❌ |
| URI | Protocol free links. | "mongodb://mongodb.net" ✅"https://google.com" ✅"google.com" ❌ |
| Username | Usernames like Twitter. | "fir4tozden" ✅"Fir4tozden" ✅"fir4t ozden" ❌ |
| Type |
|---|
| AnyObject |
| InferSchema |
| JSONSchema |
| Schema |
| TeyitOptions |
| ValidationError |
MIT License
Copyright (c) 2025 Keift
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
