Orionjs supports a variety of data types for schema definitions. This guide explains all available types and how to use them.
Basic Types
The most common types are directly mapped from JavaScript/TypeScript types:
import {schemaWithName, InferSchemaType} from '@orion-js/schema'
export const BasicTypesExample = schemaWithName('BasicTypesExample', {
stringField: {type: String}, // Text values
numberField: {type: Number}, // Numeric values (integers or floats)
booleanField: {type: Boolean}, // true/false values
dateField: {type: Date} // Date and time values
})
export type BasicTypesExampleType = InferSchemaType<typeof BasicTypesExample>
Special Types
Orionjs supports special types for common use cases:
export const SpecialTypesExample = schemaWithName('SpecialTypesExample', {
idField: {type: 'ID'}, // String or number ID (typically for database IDs)
emailField: {type: 'email'}, // Email with automatic format validation
integerField: {type: 'integer'}, // Ensures whole numbers only
blackboxField: {type: 'blackbox'} // Any object without specific schema
})
export type SpecialTypesExampleType = InferSchemaType<typeof SpecialTypesExample>
Array Types
Define arrays using TypeScript array syntax or the type option:
export const ArrayExample = schemaWithName('ArrayExample', {
stringArray: {type: [String]}, // Array of strings
numberArray: {type: [Number]}, // Alternative syntax
limitedArray: {
type: [String],
minLength: 1, // Minimum array length
maxLength: 10 // Maximum array length
}
})
export type ArrayExampleType = InferSchemaType<typeof ArrayExample>
Nested Schema Types
You can use other schemas as types for nested structures:
import {schemaWithName, InferSchemaType} from '@orion-js/schema'
export const AddressSchema = schemaWithName('AddressSchema', {
street: {type: String},
city: {type: String}
})
export type AddressType = InferSchemaType<typeof AddressSchema>
export const UserSchema = schemaWithName('UserSchema', {
name: {type: String},
// Nested schema
address: {type: AddressSchema},
// Array of schemas
alternateAddresses: {type: [AddressSchema]}
})
export type UserType = InferSchemaType<typeof UserSchema>
Enum Types
For fields with a limited set of allowed values, use the createEnum
function:
import {createEnum, schemaWithName, InferSchemaType} from '@orion-js/schema'
// Create a type-safe enum
export const StatusEnum = createEnum('StatusEnum', [
'pending',
'active',
'completed'
] as const)
// Create a numeric enum
export const FibonacciEnum = createEnum('FibonacciEnum', [1, 2, 3, 5, 8, 13] as const)
export const EnumExample = schemaWithName('EnumExample', {
status: {type: StatusEnum},
level: {type: FibonacciEnum}
})
export type EnumExampleType = InferSchemaType<typeof EnumExample>
Using createEnum
provides type safety throughout your application:
- TypeScript knows exactly which values are allowed
- The schema validation ensures only valid enum values are accepted
- GraphQL will expose the enum as a proper GraphQL enum type
Custom Types
You can create custom types by extending existing ones:
import {createType, schemaWithName, InferSchemaType} from '@orion-js/schema'
// Custom type for phone numbers
const PhoneNumber = createType({
name: 'phoneNumber',
validate(value) {
const regex = /^\+?[1-9]\d{1,14}$/
if (!regex.test(value)) {
return 'invalid-phone-number'
}
},
clean(value) {
// Remove spaces and dashes
return value.replace(/[\s-]/g, '')
}
})
export const ContactSchema = schemaWithName('ContactSchema', {
phone: {type: PhoneNumber}
})
export type ContactType = InferSchemaType<typeof ContactSchema>
Optional Types
Make any field optional using the optional parameter:
export const OptionalExample = schemaWithName('OptionalExample', {
optionalField: {type: String, optional: true} // Explicit definition
})
export type OptionalExampleType = InferSchemaType<typeof OptionalExample>