SimpleType
Package: @opra/common
SimpleType is the runtime class that represents a scalar (leaf) type in an OPRA document — strings, numbers, dates, UUIDs, and other primitive-like types. All built-in scalar types (string, integer, number, boolean, date, uuid, …) are SimpleType instances. You can also define custom scalar types by extending an existing one.
Inheritance
DocumentElement
└── DataType
└── SimpleType
Properties
Properties inherited from DataType:
| Property | Type | Description |
|---|---|---|
kind | 'SimpleType' | Always 'SimpleType'. |
name | string | undefined | Registry name. undefined for embedded (anonymous) types. |
description | string | undefined | Human-readable description. |
abstract | boolean | undefined | Whether the type is abstract. |
embedded | boolean | true when the type has no name. |
scopePattern | (string | RegExp)[] | undefined | Scopes this type is visible in. |
examples | DataTypeExample[] | undefined | Example values for documentation. |
Properties on SimpleType:
| Property | Type | Description |
|---|---|---|
base | SimpleType | undefined | The parent SimpleType this type extends from. |
attributes | Record<string, SimpleType.Attribute> | Merged attribute definitions (includes inherited attributes). |
ownAttributes | Record<string, SimpleType.Attribute> | Attribute definitions declared on this type only (not inherited). |
nameMappings | Record<string, string> | Merged name-to-name mappings across language targets (e.g. { js: 'Date', java: 'LocalDate' }). |
ownNameMappings | Record<string, string> | Name mappings declared on this type only. |
properties | any | Arbitrary constraint properties passed to codec generators (e.g. minLength, maxLength for string-based types). |
Methods
extendsFrom(baseType)
Returns true if this type extends from the given type, directly or transitively.
const stringType = document.node.getDataType('string');
const uuidType = document.node.getDataType('uuid');
uuidType.extendsFrom(stringType); // true — uuid extends string
uuidType.extendsFrom('string'); // same, by name
extendsFrom(baseType: DataType | string | Type | object): boolean
extend(properties)
Creates an inline variant of this type with overridden constraint properties. The result is a SimpleType-compatible object that inherits the parent's codec generators but applies the given property overrides.
const boundedString = stringType.extend({ minLength: 1, maxLength: 100 });
extend<T>(properties: Partial<T>): SimpleType & T
generateCodec(codec, options?, properties?)
Compiles a validation/transformation function for encoding (output) or decoding (input). The codec generator is looked up by walking up the base chain until one is found. If none is found, the validator accepts any value (isAny).
const decoder = uuidType.generateCodec('decode');
const encoder = uuidType.generateCodec('encode', { scope: 'public' });
An optional properties argument overrides constraint properties for this particular codec invocation (merged on top of this.properties):
const decoder = stringType.generateCodec('decode', null, { minLength: 5, maxLength: 50 });
generateCodec(
codec: 'encode' | 'decode',
options?: DataType.GenerateCodecOptions | null,
properties?: Record<string, any>
): Validator
inScope(scope?)
Returns true if this type is visible in the given scope. Types without a scopePattern are always in scope.
inScope(scope?: string | '*'): boolean
toJSON(options?)
Returns an OpraSchema.SimpleType plain object for schema export.
toJSON(options?: ApiDocument.ExportOptions): OpraSchema.SimpleType
SimpleType.Attribute
Attributes describe the constraint parameters a SimpleType accepts (e.g. minLength, pattern on a string type). Each attribute is a metadata entry that codec generators read from properties.
| Property | Type | Description |
|---|---|---|
format | string | undefined | Data format hint (e.g. 'integer', 'date'). |
description | string | undefined | Human-readable description of the attribute. |
default | any | Default value for the attribute if not set by the caller. |
sealed | boolean | undefined | If true, derived types cannot override this attribute. |
deprecated | boolean | string | undefined | Marks the attribute as deprecated. |
Obtaining a SimpleType instance
import { SimpleType } from '@opra/common';
const stringType = document.node.getDataType('string');
const uuidType = document.node.getDataType('uuid');
const integerType = document.node.getDataType('integer');
if (stringType instanceof SimpleType) {
console.log(stringType.name); // 'string'
console.log(stringType.attributes); // { minLength, maxLength, pattern, ... }
}