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

typeschema-dsl DSLJSON SchemaDescription
stringstring{ type: 'string' }text type
numbernumber{ type: 'number' }floating point number
integerinteger{ type: 'integer' }integer
integer aliasint{ type: 'integer' }Alias of integer
Booleanboolean{ type: 'boolean' }true/false
objectobject{ type: 'object' }Nested objects
arrayarray{ type: 'array' }array
null valuenull{ type: 'null' }null value
arbitraryany{}any type
any aliasmixed{}Alias of any

format type (string based)

typeschema-dsl DSLJSON Schema formatDescription
MailemailemailEmail address
URLurluriURL
URIuriuriURI string
UUIDuuiduuidUUID format
IP(IPv4/IPv6)ipanyOf(ipv4, ipv6)Dual stack IP
datedatedateYYYY-MM-DD
date timedatetimedate-timeISO 8601
timetimetimeHH:mm:ss
hostnamehostnamehostnamehostname
IPv4ipv4ipv4IPv4 address
IPv6ipv6ipv6IPv6 address

special type

typeschema-dsl DSLJSON SchemaDescription
binarybinarycontentEncoding: base64Base64 encoding
binary aliasbuffercontentEncoding: base64Alias of binary
ObjectIdobjectIdpattern: ^[0-9a-fA-F]{24}$MongoDB ObjectId
ObjectId aliasobjectidpattern: ^[0-9a-fA-F]{24}$Lowercase alias of objectId
HexColorhexColor`pattern: ^#([A-Fa-f0-9]{6}[A-Fa-f0-9]{3})$`
MAC addressmacAddresspattern: ^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$MAC address
Croncronpattern: ...Cron expression
Slugslugpattern: ^[a-z0-9]+(?:-[a-z0-9]+)*$URL Slug
Chinese namechineseNamepattern: ^[\u4e00-\u9fa5]{2,10}$Chinese name
Chinese textchinesepattern: ^[\u4e00-\u9fa5]+$Pure Chinese text
Email domain extensionemailDomainformat: emailEmail + domain name extension validation
Alphanumeric onlyalphanumalphanum: trueCustom AJV keyword
lowercase stringlowerlowercase: trueCustom AJV keyword
uppercase stringupperuppercase: trueCustom AJV keyword
JSON stringjsonjsonString: trueCustom AJV keyword
port numberportport: trueinteger port number
float aliasfloat{ type: 'number' }Alias of number
double aliasdouble{ type: 'number' }Alias of number
decimal aliasdecimal{ type: 'number' }Alias of number

📝 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!' });

format type

// 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

GoalRecommended formExample
Shortest schema objectPlain DSL strings({ email: 'email!' })
DSL seed plus chain methodss('...')s('string:3-32!').label('Username')
Full method discoverys.xxx() factorys.email().label('Email').require()
Isolated framework runtimeruntime.sruntime.s({ email: 'email!' })
Direct string-chain sourceString Extensions or transform'email!'.label('Email')

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:

CategoryDirect factories
Basic typess.string(), s.number(), s.integer(), s.int(), s.boolean(), s.object(), s.array(), s.any(), s.mixed()
Format typess.email(), s.url(), s.uri(), s.uuid(), s.ip(), s.ipv4(), s.ipv6(), s.date(), s.datetime(), s.time(), s.slug()
Common presetss.phone(country?), s.username(preset?), s.password(strength?)
Other built-in typesUse s('objectId!'), s('hexColor'), or s.type('objectId')

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.