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

whiteand / ts-quartet / 14367379381

09 Apr 2025 09:31PM UTC coverage: 92.631% (-4.0%) from 96.64%
14367379381

Pull #9

github

whiteand
fix: fixed tests
Pull Request #9: Standard Compatibility

471 of 517 branches covered (91.1%)

Branch coverage included in aggregate %.

88 of 161 new or added lines in 3 files covered. (54.66%)

1666 of 1790 relevant lines covered (93.07%)

72.65 hits per line

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

48.47
/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!
NEW
23
    return `${schema.toString()}`;
×
NEW
24
  }
×
25
  if (typeof schema === "bigint") {
1!
NEW
26
    return `${schema}n`;
×
NEW
27
  }
×
28
  if (schema.type === ExplanationSchemaType.And) {
1!
NEW
29
    return `and<${schema.schemas.map((t) => getExpectedTypeName(t)).join(",")}>`;
×
NEW
30
  }
×
31
  if (schema.type === ExplanationSchemaType.Any) {
1!
NEW
32
    return `any`;
×
NEW
33
  }
×
34
  if (schema.type === ExplanationSchemaType.Array) {
1!
NEW
35
    return `Array<any>`;
×
NEW
36
  }
×
37
  if (schema.type === ExplanationSchemaType.ArrayOf) {
1!
NEW
38
    return `Array<${getExpectedTypeName(schema.elementSchema)}>`;
×
NEW
39
  }
×
40
  if (schema.type === ExplanationSchemaType.Boolean) {
1!
NEW
41
    return `boolean`;
×
NEW
42
  }
×
43
  if (schema.type === ExplanationSchemaType.Finite) {
1!
NEW
44
    return `finite`;
×
NEW
45
  }
×
46
  if (schema.type === ExplanationSchemaType.Function) {
1!
NEW
47
    return `function`;
×
NEW
48
  }
×
49
  if (schema.type === ExplanationSchemaType.Max) {
1!
NEW
50
    if (schema.isExclusive) {
×
NEW
51
      return `lt<${schema.maxValue}>`;
×
NEW
52
    } else {
×
NEW
53
      return `le<${schema.maxValue}>`;
×
NEW
54
    }
×
NEW
55
  }
×
56
  if (schema.type === ExplanationSchemaType.MaxLength) {
1!
NEW
57
    if (schema.isExclusive) {
×
NEW
58
      return `lengthLt<${schema.maxLength}>`;
×
NEW
59
    }
×
NEW
60
    return `lengthLe<${schema.maxLength}>`;
×
NEW
61
  }
×
62
  if (schema.type === ExplanationSchemaType.Min) {
1!
NEW
63
    if (schema.isExclusive) {
×
NEW
64
      return `gt<${schema.minValue}>`;
×
NEW
65
    } else {
×
NEW
66
      return `ge<${schema.minValue}>`;
×
NEW
67
    }
×
NEW
68
  }
×
69
  if (schema.type === ExplanationSchemaType.MinLength) {
1!
NEW
70
    if (schema.isExclusive) {
×
NEW
71
      return `lengthGt<${schema.minLength}>`;
×
NEW
72
    }
×
NEW
73
    return `lengthGe<${schema.minLength}>`;
×
NEW
74
  }
×
75
  if (schema.type === ExplanationSchemaType.Negative) {
1!
NEW
76
    return `ge<0>`;
×
NEW
77
  }
×
78
  if (schema.type === ExplanationSchemaType.Never) {
1!
NEW
79
    return `never`;
×
NEW
80
  }
×
81
  if (schema.type === ExplanationSchemaType.Not) {
1!
NEW
82
    const inner = getExpectedTypeName(schema.schema);
×
NEW
83
    return `not<${inner}>`;
×
NEW
84
  }
×
85
  if (schema.type === ExplanationSchemaType.NotANumber) {
1!
NEW
86
    return `NaN`;
×
NEW
87
  }
×
88
  if (schema.type === ExplanationSchemaType.Number) {
1!
NEW
89
    return `number`;
×
NEW
90
  }
×
91
  if (schema.type === ExplanationSchemaType.Object) {
1!
NEW
92
    return `{ ${Object.entries(schema.propsSchemas).map((x) => `${x[0]}: ${getExpectedTypeName(x[1])}`)} }`;
×
NEW
93
  }
×
94
  if (schema.type === ExplanationSchemaType.Pair) {
1!
NEW
95
    return `pair<${getExpectedTypeName(schema.keyValueSchema)}>`;
×
NEW
96
  }
×
97
  if (schema.type === ExplanationSchemaType.Positive) {
1!
NEW
98
    return `gt<0>`;
×
NEW
99
  }
×
100
  if (schema.type === ExplanationSchemaType.SafeInteger) {
1!
NEW
101
    return `safeInteger`;
×
NEW
102
  }
×
103
  if (schema.type === ExplanationSchemaType.String) {
1✔
104
    return `string`;
1✔
105
  }
1!
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

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

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

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

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

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