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

oprajs / opra / 14419802659

12 Apr 2025 12:55PM UTC coverage: 82.088% (+4.2%) from 77.908%
14419802659

push

github

web-flow
Merge pull request #27 from oprajs/dev

Dev

3458 of 4432 branches covered (78.02%)

Branch coverage included in aggregate %.

16 of 16 new or added lines in 10 files covered. (100.0%)

1793 existing lines in 129 files now uncovered.

29424 of 35625 relevant lines covered (82.59%)

183.2 hits per line

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

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

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

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

1✔
33
  export interface InitArguments extends DataType.Metadata {}
1✔
34

1✔
35
  export interface GenerateCodecOptions extends ValidationOptions {
1✔
36
    documentElement?: DocumentElement;
1✔
37
    scope?: string;
1✔
38
    caseInSensitive?: boolean;
1✔
39
    partial?: boolean | 'deep';
1✔
40
    projection?: string[] | FieldsProjection | '*';
1✔
41
    keepKeyFields?: boolean;
1✔
42
    ignoreReadonlyFields?: boolean;
1✔
43
    ignoreWriteonlyFields?: boolean;
1✔
44
    allowPatchOperators?: boolean;
1✔
45
  }
1✔
46
}
1✔
47

1✔
48
interface DataTypeStatic {
1✔
49
  new (
1✔
50
    owner: DocumentElement,
1✔
51
    args?: DataType.InitArguments,
1✔
52
    context?: DocumentInitContext,
1✔
53
  ): DataType;
1✔
54

1✔
55
  prototype: DataType;
1✔
56
}
1✔
57

1✔
58
/**
1✔
59
 * Type definition of DataType prototype
1✔
60
 * @interface DataType
1✔
61
 */
1✔
62
export interface DataType extends DataTypeClass {}
1✔
63

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

1✔
93
/**
1✔
94
 *
1✔
95
 * @class DataType
1✔
96
 */
1✔
97
abstract class DataTypeClass extends DocumentElement {
1✔
98
  declare readonly kind: OpraSchema.DataType.Kind;
1✔
99
  declare readonly owner: DocumentElement;
1✔
100
  declare readonly scopePattern?: (string | RegExp)[];
1✔
101
  declare readonly name?: string;
1✔
102
  declare readonly description?: string;
1✔
103
  declare readonly abstract?: boolean;
1✔
104
  declare readonly examples?: OpraSchema.DataTypeExample[];
1✔
105

1✔
106
  abstract generateCodec(
1✔
107
    codec: 'encode' | 'decode',
1✔
108
    options?: DataType.GenerateCodecOptions,
1✔
109
  ): Validator;
1✔
110

1✔
111
  get embedded(): any {
1✔
112
    return !this.name;
6✔
113
  }
6✔
114

1✔
115
  abstract extendsFrom(baseType: DataType | string | Type | object): boolean;
1✔
116

1✔
117
  inScope(scope?: string | '*'): boolean {
1✔
118
    return testScopeMatch(scope, this.scopePattern);
8,589✔
119
  }
8,589✔
120

1✔
121
  toJSON(options?: ApiDocument.ExportOptions): OpraSchema.DataType {
1✔
122
    /** Locate base model which is not available for given scope */
4,226✔
123
    const outOfScope = this._locateBase(dt => !dt.inScope(options?.scope));
4,226✔
124
    if (outOfScope) {
4,226!
125
      const baseName = this.node.getDataTypeNameWithNs(outOfScope);
×
126
      throw new TypeError(
×
UNCOV
127
        `"${baseName}" model is not available for "${options?.scope || 'null'}" scope`,
×
UNCOV
128
      );
×
UNCOV
129
    }
×
130
    return omitUndefined({
4,226✔
131
      kind: this.kind,
4,226✔
132
      description: this.description,
4,226✔
133
      abstract: this.abstract,
4,226✔
134
      examples: this.examples,
4,226✔
135
    }) as OpraSchema.DataType;
4,226✔
136
  }
4,226✔
137

1✔
138
  toString(): string {
1✔
139
    return `[${Object.getPrototypeOf(this).constructor.name} ${this.name || '#Embedded'}]`;
×
UNCOV
140
  }
×
141

1✔
142
  protected abstract _locateBase(
1✔
143
    callback: (base: DataType) => boolean,
1✔
144
  ): DataType | undefined;
1✔
145

1✔
146
  [nodeInspectCustom](): string {
1✔
147
    return (
×
UNCOV
148
      `[${colorFgYellow + Object.getPrototypeOf(this).constructor.name + colorReset}` +
×
UNCOV
149
      ` ${colorFgMagenta + this.name + colorReset}]`
×
UNCOV
150
    );
×
UNCOV
151
  }
×
152
}
1✔
153

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

© 2026 Coveralls, Inc