Skip to main content

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:

PropertyTypeDescription
kind'SimpleType'Always 'SimpleType'.
namestring | undefinedRegistry name. undefined for embedded (anonymous) types.
descriptionstring | undefinedHuman-readable description.
abstractboolean | undefinedWhether the type is abstract.
embeddedbooleantrue when the type has no name.
scopePattern(string | RegExp)[] | undefinedScopes this type is visible in.
examplesDataTypeExample[] | undefinedExample values for documentation.

Properties on SimpleType:

PropertyTypeDescription
baseSimpleType | undefinedThe parent SimpleType this type extends from.
attributesRecord<string, SimpleType.Attribute>Merged attribute definitions (includes inherited attributes).
ownAttributesRecord<string, SimpleType.Attribute>Attribute definitions declared on this type only (not inherited).
nameMappingsRecord<string, string>Merged name-to-name mappings across language targets (e.g. { js: 'Date', java: 'LocalDate' }).
ownNameMappingsRecord<string, string>Name mappings declared on this type only.
propertiesanyArbitrary 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.

PropertyTypeDescription
formatstring | undefinedData format hint (e.g. 'integer', 'date').
descriptionstring | undefinedHuman-readable description of the attribute.
defaultanyDefault value for the attribute if not set by the caller.
sealedboolean | undefinedIf true, derived types cannot override this attribute.
deprecatedboolean | string | undefinedMarks 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, ... }
}

ApiDocument · ComplexType