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

whiteand / ts-quartet / 14367094160

09 Apr 2025 09:13PM UTC coverage: 91.456% (-5.2%) from 96.64%
14367094160

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 153 new or added lines in 5 files covered. (20.26%)

8 existing lines in 1 file now uncovered.

1605 of 1778 relevant lines covered (90.27%)

72.67 hits per line

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

25.33
/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 number`;
×
NEW
45
  }
×
NEW
46
  if (schema.type === ExplanationSchemaType.Function) {
×
NEW
47
    return `function`;
×
NEW
48
  }
×
NEW
49
  if (schema.type === ExplanationSchemaType.Max) {
×
NEW
50
    return `(${schema.isExclusive ? ">" : ">="} ${schema.maxValue})`;
×
NEW
51
  }
×
NEW
52
  if (schema.type === ExplanationSchemaType.MaxLength) {
×
NEW
53
    return `{ length: ${schema.isExclusive ? ">" : ">="} ${schema.maxLength} }`;
×
NEW
54
  }
×
NEW
55
  if (schema.type === ExplanationSchemaType.Min) {
×
NEW
56
    return `(${schema.isExclusive ? "<" : "<="} ${schema.minValue})`;
×
NEW
57
  }
×
NEW
58
  if (schema.type === ExplanationSchemaType.MinLength) {
×
NEW
59
    return `{ length: ${schema.isExclusive ? "<" : "<="} ${schema.minLength}}`;
×
NEW
60
  }
×
NEW
61
  if (schema.type === ExplanationSchemaType.Negative) {
×
NEW
62
    return `(< 0)`;
×
NEW
63
  }
×
NEW
64
  if (schema.type === ExplanationSchemaType.Never) {
×
NEW
65
    return `never`;
×
NEW
66
  }
×
NEW
67
  if (schema.type === ExplanationSchemaType.Not) {
×
NEW
68
    const inner = getExpectedTypeName(schema.schema);
×
NEW
69
    return `not (${inner})`;
×
NEW
70
  }
×
NEW
71
  if (schema.type === ExplanationSchemaType.NotANumber) {
×
NEW
72
    return `NaN`;
×
NEW
73
  }
×
NEW
74
  if (schema.type === ExplanationSchemaType.Number) {
×
NEW
75
    return `number`;
×
NEW
76
  }
×
NEW
77
  if (schema.type === ExplanationSchemaType.Object) {
×
NEW
78
    return `{ ${Object.entries(schema.propsSchemas).map((x) => `${x[0]}: ${getExpectedTypeName(x[1])}`)} }`;
×
NEW
79
  }
×
NEW
80
  if (schema.type === ExplanationSchemaType.Pair) {
×
NEW
81
    return `pair<${getExpectedTypeName(schema.keyValueSchema)}>`;
×
NEW
82
  }
×
NEW
83
  if (schema.type === ExplanationSchemaType.Positive) {
×
NEW
84
    return `(> 0)`;
×
NEW
85
  }
×
NEW
86
  if (schema.type === ExplanationSchemaType.SafeInteger) {
×
NEW
87
    return `safe integer`;
×
NEW
88
  }
×
NEW
89
  if (schema.type === ExplanationSchemaType.String) {
×
NEW
90
    return `string`;
×
NEW
91
  }
×
NEW
92
  if (schema.type === ExplanationSchemaType.Symbol) {
×
NEW
93
    return `symbol`;
×
NEW
94
  }
×
NEW
95
  if (schema.type === ExplanationSchemaType.Test) {
×
NEW
96
    return `test<${schema.description}>`;
×
NEW
97
  }
×
NEW
98
  if (schema.type === ExplanationSchemaType.Variant) {
×
NEW
99
    return `oneOf<${schema.variants.map((x) => getExpectedTypeName(x)).join(", ")}>`;
×
NEW
100
  }
×
NEW
101
  if (schema.type === ExplanationSchemaType.Custom) {
×
NEW
102
    return `custom<${schema.description}>`;
×
NEW
103
  }
×
104

NEW
105
  return JSON.stringify(schema);
×
NEW
106
}
×
107

NEW
108
function getMessage(explanation: IExplanation): string {
×
NEW
109
  const { schema } = explanation;
×
110

NEW
111
  return `expected type: ${getExpectedTypeName(schema)}`;
×
NEW
112
}
×
113

NEW
114
function getPath(
×
NEW
115
  explanation: IExplanation
×
NEW
116
): ReadonlyArray<PropertyKey | StandardSchemaV1.PathSegment> | undefined {
×
NEW
117
  return [...explanation.path];
×
NEW
118
}
×
119

120
export function eCompileSchema<T = Z>(
1✔
121
  schema: TSchema
81✔
122
): CompilationResult<T, Z> {
81✔
123
  const explanator: (value: Z, path: KeyType[]) => null | IExplanation[] =
81✔
124
    getExplanator(schema);
81✔
125
  const explanations: IExplanation[] = [];
81✔
126
  function validator(value: Z) {
81✔
127
    const explanationsOrNull = explanator(value, []);
1,153✔
128
    if (explanationsOrNull) {
1,153✔
129
      (
875✔
130
        validator as unknown as CompilationResult<T, IExplanation>
875✔
131
      ).explanations = explanationsOrNull;
875✔
132
      return false;
875✔
133
    } else {
1,153✔
134
      (
278✔
135
        validator as unknown as CompilationResult<T, IExplanation>
278✔
136
      ).explanations = [];
278✔
137
      return true;
278✔
138
    }
278✔
139
  }
1,153✔
140

141
  const res = Object.assign(validator as Validator<T>, {
81✔
142
    explanations,
81✔
143
    schema,
81✔
144
    cast() {
81✔
UNCOV
145
      return this as Z;
×
UNCOV
146
    },
×
147
  }) as Z;
81✔
148
  res["~standard"] = implStandard(
81✔
149
    res as CompilationResult<T, IExplanation>,
81✔
150
    (explanations) => {
81✔
NEW
UNCOV
151
      return explanations.map((explanation) => {
×
NEW
UNCOV
152
        const message = getMessage(explanation);
×
NEW
UNCOV
153
        const path = getPath(explanation);
×
NEW
UNCOV
154
        return {
×
155
          /** The error message of the issue. */
NEW
UNCOV
156
          message,
×
157
          /** The path of the issue, if any. */
NEW
UNCOV
158
          path,
×
NEW
159
        };
×
NEW
160
      });
×
NEW
161
    }
×
162
  );
81✔
163
  return res;
81✔
164
}
81✔
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