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

whiteand / ts-quartet / 14367155701

09 Apr 2025 09:16PM UTC coverage: 90.893% (-5.7%) from 96.64%
14367155701

Pull #9

github

whiteand
fix: unified errors
Pull Request #9: Standard Compatibility

461 of 481 branches covered (95.84%)

Branch coverage included in aggregate %.

31 of 167 new or added lines in 5 files covered. (18.56%)

1605 of 1792 relevant lines covered (89.56%)

72.1 hits per line

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

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

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

NEW
119
  return JSON.stringify(schema);
×
NEW
120
}
×
121

NEW
122
function getMessage(explanation: IExplanation): string {
×
NEW
123
  const { schema } = explanation;
×
124

NEW
125
  return `expected type: ${getExpectedTypeName(schema)}`;
×
NEW
126
}
×
127

NEW
128
function getPath(
×
NEW
129
  explanation: IExplanation
×
NEW
130
): ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment> | undefined {
×
NEW
131
  return [...explanation.path];
×
NEW
132
}
×
133

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

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