schema-dsl complete type list
This page lists the built-in DSL types and shows how to use the same type through the recommended authoring entries: plain DSL strings, s('...') builder seeds, and s.xxx() factories.
📊 Supported types
basic type
special type
📝 Type usage examples
basic type
import { s } from 'schema-dsl/pure';
// string
const schema1 = s({ name: 'string' });
// number
const schema2 = s({ age: 'number' });
// integer
const schema3 = s({ count: 'integer' });
// boolean
const schema4 = s({ active: 'boolean' });
// object
const schema5 = s({
user: {
name: 'string',
age: 'number'
}
});
// array
const schema6 = s({ tags: 'array<string>' });
// object array
const schema6b = s({
items: s.array({
name: 'string!',
quantity: 'number:1-999!'
})
});
//null value
const schema7 = s({ value: 'null' });
// any type
const schema8 = s({ data: 'any' });
Parameterized DSL types
//Mobile phone number (default cn)
const schema1 = s({ mobile: 'phone:cn!' });
// ID card
const schema2 = s({ idCard: 'idCard:cn!' });
// credit card
const schema3 = s({ card: 'creditCard:visa!' });
// license plate number
const schema4 = s({ plate: 'licensePlate:cn!' });
// postal code
const schema5 = s({ zip: 'postalCode:cn!' });
// passport
const schema6 = s({ passportNo: 'passport:cn!' });
// Mail
const schema1 = s({ email: 'email!' });
// URL
const schema2 = s({ website: 'url' });
// UUID
const schema3 = s({ id: 'uuid!' });
// date
const schema4 = s({ birthday: 'date' });
// date time
const schema5 = s({ created_at: 'datetime!' });
// time
const schema6 = s({ start_time: 'time' });
// IP address
const schema7 = s({
ipv4_addr: 'ipv4',
ipv6_addr: 'ipv6'
});
special type
//Binary data (Base64)
const schema = s({
avatar: 'binary' // Avatar image (Base64 encoding)
});
Authoring entry matrix
Factory examples:
import { s } from 'schema-dsl/pure';
const schema = s({
name: s.string().min(1).max(50).require(),
age: s.number().min(18).max(120),
email: s.email().label('Email').require(),
tags: s.array('string:1-30').min(1).max(10),
lines: s.array({ name: 'string!', quantity: 'number:1-999!' }),
status: s.enum('active', 'inactive', 'pending').default('active')
});
Factory support boundary
Not every DSL type has a same-name s.xxx() factory. Built-in factories cover the most common types and entry points:
s.array(item) and .items(item) accept a DSL string, builder, DSL object, or standard JSON Schema:
s.array('string:1-30')
s.array(s.string().min(1).require())
s.array({ name: 'string!', quantity: 'number:1-999!' })
s.array({ type: 'string', minLength: 1 }) // standard JSON Schema
s.array({ enum: ['small', 'large'] }) // JSON Schema fragment without type is preserved
If an object-array child field is named like a JSON Schema keyword, such as enum, pattern, or minimum, wrap the DSL object with s({ ... }) to make the intent explicit:
s.array(s({
enum: 'string!',
pattern: 'string'
}))
❓ FAQ
Q1: How should I express alternative field types?
A: schema-dsl splits this type of requirements into two categories:
- Single field cross-type union: use
types: syntax
- Make conditional branches based on other fields: use
s.match()
const schema = s({
value: 'types:string|number',
contactType: 'email|phone',
contact: s.match('contactType', {
email: 'email!',
phone: 'phone:cn!'
})
});
Q2: Why is integer not number().integer()?
A: schema-dsl uses JSON Schema standard, integer is an independent type.
Q3: Doesn't abbreviation support?
A: Abbreviations such as s/n/i/b are not supported. Use the complete type name (string/number/integer/boolean) to reduce learning costs.
Corresponding sample file
Example entry: type-reference.ts
Description: Use a schema to string together commonly used built-in types, parameterized DSL types and runtime error paths to facilitate quick validation of the actual support range.