• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

panates / opra / 25956913535

16 May 2026 08:06AM UTC coverage: 83.031% (+0.004%) from 83.027%
25956913535

push

github

erayhanoglu
1.28.4

3730 of 4808 branches covered (77.58%)

Branch coverage included in aggregate %.

33693 of 40263 relevant lines covered (83.68%)

222.65 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

89.89
/packages/common/src/document/data-type/data-type.ts
1
import { omitUndefined } from '@jsopen/objects';
1✔
2
import { asMutable, type StrictOmit, type Type } from 'ts-gems';
1✔
3
import type { ValidationOptions, Validator } from 'valgen';
1✔
4
import { FieldsProjection } from '../../helpers/index.js';
1✔
5
import type { DataTypeBase } from '../../schema/data-type/data-type.interface.js';
1✔
6
import { OpraSchema } from '../../schema/index.js';
1✔
7
import type { ApiDocument } from '../api-document.js';
1✔
8
import { DocumentElement } from '../common/document-element.js';
1✔
9
import { DocumentInitContext } from '../common/document-init-context.js';
1✔
10
import { CLASS_NAME_PATTERN } from '../constants.js';
1✔
11
import {
1✔
12
  colorFgMagenta,
1✔
13
  colorFgYellow,
1✔
14
  colorReset,
1✔
15
  nodeInspectCustom,
1✔
16
} from '../utils/inspect.util.js';
1✔
17
import { testScopeMatch } from '../utils/test-scope-match.js';
1✔
18
import type { ApiField } from './api-field.js';
1✔
19

1✔
20
/**
1✔
21
 * @namespace DataType
1✔
22
 */
1✔
23
export namespace DataType {
1✔
24
  export interface Metadata extends DataTypeBase {
1✔
25
    name?: string;
1✔
26
    scopePattern?: (string | RegExp) | (string | RegExp)[];
1✔
27
  }
1✔
28

1✔
29
  export interface Options extends Partial<
1✔
30
    StrictOmit<Metadata, 'kind' | 'examples'>
1✔
31
  > {
1✔
32
    embedded?: boolean;
1✔
33
  }
1✔
34

1✔
35
  export interface InitArguments extends DataType.Metadata {}
1✔
36

1✔
37
  export interface GenerateCodecOptions extends ValidationOptions {
1✔
38
    documentElement?: DocumentElement;
1✔
39
    scope?: string;
1✔
40
    caseInSensitive?: boolean;
1✔
41
    partial?: boolean | 'deep';
1✔
42
    projection?: string[] | FieldsProjection | '*';
1✔
43
    keepKeyFields?: boolean;
1✔
44
    ignoreReadonlyFields?: boolean;
1✔
45
    ignoreWriteonlyFields?: boolean;
1✔
46
    allowPatchOperators?: boolean;
1✔
47
    allowNullOptionals?: boolean;
1✔
48
    fieldHook?: (
1✔
49
      field: ApiField,
1✔
50
      currentPath: string,
1✔
51
      defaultGenerator: () => Validator,
1✔
52
    ) => Validator;
1✔
53
  }
1✔
54
}
1✔
55

1✔
56
interface DataTypeStatic {
1✔
57
  new (
1✔
58
    owner: DocumentElement,
1✔
59
    args?: DataType.InitArguments,
1✔
60
    context?: DocumentInitContext,
1✔
61
  ): DataType;
1✔
62

1✔
63
  prototype: DataType;
1✔
64
}
1✔
65

1✔
66
/**
1✔
67
 * Type definition of DataType prototype
1✔
68
 * @interface DataType
1✔
69
 */
1✔
70
export interface DataType extends DataTypeClass {}
1✔
71

1✔
72
/**
1✔
73
 * DataType constructor
1✔
74
 */
1✔
75
export const DataType = function (
1✔
76
  this: DataType,
4,902✔
77
  owner: DocumentElement,
4,902✔
78
  initArgs: DataType.InitArguments,
4,902✔
79
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
4,902✔
80
  context?: DocumentInitContext,
4,902✔
81
) {
4,902✔
82
  if (!this)
4,902✔
83
    throw new TypeError('"this" should be passed to call class constructor');
4,902!
84
  if (initArgs?.name && !CLASS_NAME_PATTERN.test(initArgs.name)) {
4,902!
85
    throw new TypeError(`"${initArgs.name}" is not a valid DataType name`);
×
86
  }
×
87
  DocumentElement.call(this, owner);
4,902✔
88
  const _this = asMutable(this);
4,902✔
89
  _this.kind = initArgs.kind;
4,902✔
90
  _this.scopePattern = initArgs.scopePattern
4,902✔
91
    ? Array.isArray(initArgs.scopePattern)
8!
92
      ? initArgs.scopePattern
×
93
      : [initArgs.scopePattern]
8✔
94
    : undefined;
4,894✔
95
  _this.name = initArgs.name;
4,902✔
96
  _this.description = initArgs.description;
4,902✔
97
  _this.abstract = initArgs.abstract;
4,902✔
98
  _this.examples = initArgs.examples;
4,902✔
99
} as Function as DataTypeStatic;
4,902✔
100

1✔
101
/**
1✔
102
 *
1✔
103
 * @class DataType
1✔
104
 */
1✔
105
abstract class DataTypeClass extends DocumentElement {
1✔
106
  declare readonly kind: OpraSchema.DataType.Kind;
1✔
107
  declare readonly owner: DocumentElement;
1✔
108
  declare readonly scopePattern?: (string | RegExp)[];
1✔
109
  declare readonly name?: string;
1✔
110
  declare readonly description?: string;
1✔
111
  declare readonly abstract?: boolean;
1✔
112
  declare readonly examples?: OpraSchema.DataTypeExample[];
1✔
113

1✔
114
  abstract generateCodec(
1✔
115
    codec: 'encode' | 'decode',
1✔
116
    options?: DataType.GenerateCodecOptions,
1✔
117
    properties?: any,
1✔
118
  ): Validator;
1✔
119

1✔
120
  get embedded(): any {
1✔
121
    return !this.name;
6✔
122
  }
6✔
123

1✔
124
  abstract extendsFrom(baseType: DataType | string | Type | object): boolean;
1✔
125

1✔
126
  inScope(scope?: string | '*'): boolean {
1✔
127
    return testScopeMatch(scope, this.scopePattern);
10,250✔
128
  }
10,250✔
129

1✔
130
  toJSON(options?: ApiDocument.ExportOptions): OpraSchema.DataType {
1✔
131
    /* Locate base model which is not available for given scope */
5,789✔
132
    const outOfScope = this._locateBase(dt => !dt.inScope(options?.scope));
5,789✔
133
    if (outOfScope) {
5,789!
134
      const baseName = this.node.getDataTypeNameWithNs(outOfScope);
×
135
      throw new TypeError(
×
136
        `"${baseName}" model is not available for "${options?.scope || 'null'}" scope`,
×
137
      );
×
138
    }
×
139
    return omitUndefined({
5,789✔
140
      kind: this.kind,
5,789✔
141
      description: this.description,
5,789✔
142
      abstract: this.abstract,
5,789✔
143
      examples: this.examples,
5,789✔
144
    }) as OpraSchema.DataType;
5,789✔
145
  }
5,789✔
146

1✔
147
  toString(): string {
1✔
148
    return `[${Object.getPrototypeOf(this).constructor.name} ${this.name || '#Embedded'}]`;
4,084!
149
  }
4,084✔
150

1✔
151
  protected abstract _locateBase(
1✔
152
    callback: (base: DataType) => boolean,
1✔
153
  ): DataType | undefined;
1✔
154

1✔
155
  [nodeInspectCustom](): string {
1✔
156
    return (
×
157
      `[${colorFgYellow + Object.getPrototypeOf(this).constructor.name + colorReset}` +
×
158
      ` ${colorFgMagenta + this.name + colorReset}]`
×
159
    );
×
160
  }
×
161
}
1✔
162

1✔
163
DataType.prototype = DataTypeClass.prototype;
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc