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

whiteand / ts-quartet / 15902742959

26 Jun 2025 01:09PM UTC coverage: 92.626% (-0.005%) from 92.631%
15902742959

push

github

whiteand
added bigint

480 of 526 branches covered (91.25%)

Branch coverage included in aggregate %.

62 of 65 new or added lines in 10 files covered. (95.38%)

1693 of 1820 relevant lines covered (93.02%)

74.69 hits per line

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

47.74
/src/compilers/eCompiler/eCompileSchema.ts
1
/* tslint:disable:object-literal-sort-keys */
2
import { StandardSchemaV1 } from "@standard-schema/spec";
3
import {
1✔
4
  ExplanationSchemaType,
5
  IExplanation,
6
  TExplanationSchema,
7
} from "../../explanations";
8
import { Z } from "../../types";
9
import { CompilationResult, TSchema, Validator } from "../../types";
10
import { implStandard } from "../implStandard";
1✔
11
import { getExplanator } from "./getExplanator";
1✔
12

13
function getExpectedTypeName(schema: TExplanationSchema): string {
1✔
14
  if (schema === undefined) return `undefined`;
1!
15
  if (schema === null) return `null`;
1!
16
  if (
1✔
17
    typeof schema === "boolean" ||
1✔
18
    typeof schema === "number" ||
1✔
19
    typeof schema === "string"
1✔
20
  )
21
    return `${JSON.stringify(schema)}`;
1!
22
  if (typeof schema === "symbol") {
1!
23
    return `${schema.toString()}`;
×
24
  }
×
25
  if (typeof schema === "bigint") {
1!
26
    return `${schema}n`;
×
27
  }
×
28
  if (schema.type === ExplanationSchemaType.And) {
1!
29
    return `and<${schema.schemas.map((t) => getExpectedTypeName(t)).join(",")}>`;
×
30
  }
×
31
  if (schema.type === ExplanationSchemaType.Any) {
1!
32
    return `any`;
×
33
  }
×
34
  if (schema.type === ExplanationSchemaType.Array) {
1!
35
    return `Array<any>`;
×
36
  }
×
37
  if (schema.type === ExplanationSchemaType.ArrayOf) {
1!
38
    return `Array<${getExpectedTypeName(schema.elementSchema)}>`;
×
39
  }
×
40
  if (schema.type === ExplanationSchemaType.Boolean) {
1!
41
    return `boolean`;
×
42
  }
×
43
  if (schema.type === ExplanationSchemaType.Finite) {
1!
44
    return `finite`;
×
45
  }
×
46
  if (schema.type === ExplanationSchemaType.Function) {
1!
47
    return `function`;
×
48
  }
×
49
  if (schema.type === ExplanationSchemaType.Max) {
1!
50
    if (schema.isExclusive) {
×
51
      return `lt<${schema.maxValue}>`;
×
52
    } else {
×
53
      return `le<${schema.maxValue}>`;
×
54
    }
×
55
  }
×
56
  if (schema.type === ExplanationSchemaType.MaxLength) {
1!
57
    if (schema.isExclusive) {
×
58
      return `lengthLt<${schema.maxLength}>`;
×
59
    }
×
60
    return `lengthLe<${schema.maxLength}>`;
×
61
  }
×
62
  if (schema.type === ExplanationSchemaType.Min) {
1!
63
    if (schema.isExclusive) {
×
64
      return `gt<${schema.minValue}>`;
×
65
    } else {
×
66
      return `ge<${schema.minValue}>`;
×
67
    }
×
68
  }
×
69
  if (schema.type === ExplanationSchemaType.MinLength) {
1!
70
    if (schema.isExclusive) {
×
71
      return `lengthGt<${schema.minLength}>`;
×
72
    }
×
73
    return `lengthGe<${schema.minLength}>`;
×
74
  }
×
75
  if (schema.type === ExplanationSchemaType.Negative) {
1!
76
    return `ge<0>`;
×
77
  }
×
78
  if (schema.type === ExplanationSchemaType.Never) {
1!
79
    return `never`;
×
80
  }
×
81
  if (schema.type === ExplanationSchemaType.Not) {
1!
82
    const inner = getExpectedTypeName(schema.schema);
×
83
    return `not<${inner}>`;
×
84
  }
×
85
  if (schema.type === ExplanationSchemaType.NotANumber) {
1!
86
    return `NaN`;
×
87
  }
×
88
  if (schema.type === ExplanationSchemaType.Number) {
1!
89
    return `number`;
×
90
  }
×
91
  if (schema.type === ExplanationSchemaType.Object) {
1!
92
    return `{ ${Object.entries(schema.propsSchemas).map((x) => `${x[0]}: ${getExpectedTypeName(x[1])}`)} }`;
×
93
  }
×
94
  if (schema.type === ExplanationSchemaType.Pair) {
1!
95
    return `pair<${getExpectedTypeName(schema.keyValueSchema)}>`;
×
96
  }
×
97
  if (schema.type === ExplanationSchemaType.Positive) {
1!
98
    return `gt<0>`;
×
99
  }
×
100
  if (schema.type === ExplanationSchemaType.SafeInteger) {
1!
101
    return `safeInteger`;
×
102
  }
×
103
  if (schema.type === ExplanationSchemaType.String) {
1✔
104
    return `string`;
1✔
105
  }
1!
NEW
106
  if (schema.type === ExplanationSchemaType.BigInt) {
×
NEW
107
    return `bigint`;
×
NEW
108
  }
×
109
  if (schema.type === ExplanationSchemaType.Symbol) {
×
110
    return `symbol`;
×
111
  }
×
112
  if (schema.type === ExplanationSchemaType.Test) {
×
113
    return `test<${schema.description}>`;
×
114
  }
×
115
  if (schema.type === ExplanationSchemaType.Variant) {
×
116
    return `oneOf<${schema.variants.map((x) => getExpectedTypeName(x)).join(", ")}>`;
×
117
  }
×
118
  if (schema.type === ExplanationSchemaType.Custom) {
×
119
    return `custom<${schema.description}>`;
×
120
  }
×
121

122
  return JSON.stringify(schema);
×
123
}
×
124

125
function getMessage(explanation: IExplanation): string {
1✔
126
  const { schema } = explanation;
1✔
127

128
  return `expected type: ${getExpectedTypeName(schema)}`;
1✔
129
}
1✔
130

131
function getPath(
1✔
132
  explanation: IExplanation
1✔
133
): ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment> | undefined {
1✔
134
  return [...explanation.path];
1✔
135
}
1✔
136

137
export function eCompileSchema<T = Z>(
1✔
138
  schema: TSchema
88✔
139
): CompilationResult<T, Z> {
88✔
140
  const explanator: (value: Z, path: KeyType[]) => null | IExplanation[] =
88✔
141
    getExplanator(schema);
88✔
142
  const explanations: IExplanation[] = [];
88✔
143
  function validator(value: Z) {
88✔
144
    const explanationsOrNull = explanator(value, []);
1,190✔
145
    if (explanationsOrNull) {
1,190✔
146
      (
906✔
147
        validator as unknown as CompilationResult<T, IExplanation>
906✔
148
      ).explanations = explanationsOrNull;
906✔
149
      return false;
906✔
150
    } else {
1,189✔
151
      (
284✔
152
        validator as unknown as CompilationResult<T, IExplanation>
284✔
153
      ).explanations = [];
284✔
154
      return true;
284✔
155
    }
284✔
156
  }
1,190✔
157

158
  const res = Object.assign(validator as Validator<T>, {
88✔
159
    explanations,
88✔
160
    schema,
88✔
161
    cast() {
88✔
162
      return this as Z;
×
163
    },
×
164
  }) as Z;
88✔
165
  res["~standard"] = implStandard(
88✔
166
    res as CompilationResult<T, IExplanation>,
88✔
167
    (explanations) => {
88✔
168
      return explanations.map((explanation) => {
1✔
169
        const message = getMessage(explanation);
1✔
170
        const path = getPath(explanation);
1✔
171
        return {
1✔
172
          /** The error message of the issue. */
173
          message,
1✔
174
          /** The path of the issue, if any. */
175
          path,
1✔
176
        };
1✔
177
      });
1✔
178
    }
1✔
179
  );
88✔
180
  return res;
88✔
181
}
88✔
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