kanagata 0.3.0 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/schema.js +99 -2
  2. package/package.json +1 -1
package/dist/schema.js CHANGED
@@ -108,8 +108,10 @@ function parts(cx, v, at) {
108
108
  // and a member is narrowed by the property only where the member itself fixes it.
109
109
  for (const key of ["oneOf", "anyOf"]) {
110
110
  const members = v.get(key);
111
- if (Array.isArray(members))
112
- out.push(union(admitted(members, list(cx, members, child(at, key)))));
111
+ if (!Array.isArray(members))
112
+ continue;
113
+ const tys = admitted(members, list(cx, members, child(at, key)));
114
+ out.push(union(key === "oneOf" ? exclusive(cx, members, tys, child(at, key)) : tys));
113
115
  }
114
116
  return loose ? union([inter(out), NULL]) : inter(out);
115
117
  }
@@ -141,6 +143,101 @@ function requiredKeys(cx, s) {
141
143
  ...cx.withBase(target.base, () => requiredKeys(cx, target.value)),
142
144
  ];
143
145
  }
146
+ /**
147
+ * oneOf admits a value only one member admits. A member that asks nothing but some
148
+ * keys (`{ required: [a] }`) does not admit exactly the objects without one of them.
149
+ * So among such members, each lacks one of the keys each other requires: `b?: never`,
150
+ * or `({ b?: never } | { c?: never })` for `required: [b, c]`, with `b?: unknown;
151
+ * c?: unknown` so that an object literal with one of them is not excess. One that has
152
+ * all of another's keys admits nothing. Other members stay as they are: TypeScript
153
+ * cannot say what they do not admit.
154
+ */
155
+ function exclusive(cx, members, tys, at) {
156
+ const keys = members.map((m, i) => keysOnly(cx, m, child(at, String(i))));
157
+ const without = (key, ty) => ({ key, ty, optional: true });
158
+ return tys.map((t, i) => {
159
+ const own = keys[i];
160
+ if (own === undefined)
161
+ return t;
162
+ // Each key another member requires: never here, or unknown where only one of several must lack.
163
+ const others = new Map();
164
+ const either = [];
165
+ for (const [j, other] of keys.entries()) {
166
+ if (j === i || other === undefined)
167
+ continue;
168
+ const missing = other.filter((k) => !own.includes(k));
169
+ if (missing.length === 0)
170
+ return NEVER;
171
+ if (missing.length === 1) {
172
+ others.set(missing[0], NEVER);
173
+ continue;
174
+ }
175
+ for (const k of missing)
176
+ if (!others.has(k))
177
+ others.set(k, UNKNOWN);
178
+ either.push(union(missing.map((k) => ({ kind: "object", props: [without(k, NEVER)] }))));
179
+ }
180
+ if (others.size === 0)
181
+ return t;
182
+ const props = [...others].map(([k, ty]) => without(k, ty));
183
+ return inter([t, { kind: "object", props }, ...either]);
184
+ });
185
+ }
186
+ /** Keywords that do not change which values are valid. */
187
+ const ANNOTATIONS = new Set([
188
+ "title",
189
+ "description",
190
+ "$comment",
191
+ "default",
192
+ "example",
193
+ "examples",
194
+ "deprecated",
195
+ "externalDocs",
196
+ "xml",
197
+ ]);
198
+ const isAnnotation = (k) => ANNOTATIONS.has(k) || k.startsWith("x-");
199
+ /**
200
+ * The keys a schema requires, when that is all it asks of an object: a non-empty
201
+ * `required`, with at most `type: object`, properties that admit any value and
202
+ * annotations; or a `$ref`, with annotations at most, to such a schema.
203
+ */
204
+ function keysOnly(cx, s, at) {
205
+ if (!(s instanceof JsonObject))
206
+ return undefined;
207
+ const r = s.str("$ref");
208
+ if (r !== undefined)
209
+ return s.entries.every(([k]) => k === "$ref" || isAnnotation(k))
210
+ ? follow(cx, r, at, (t, where) => keysOnly(cx, t, where))
211
+ : undefined;
212
+ const props = child(at, "properties");
213
+ const allowed = s.entries.every(([k, x]) => k === "required" ||
214
+ isAnnotation(k) ||
215
+ (k === "type" && x === "object") ||
216
+ (k === "properties" &&
217
+ x instanceof JsonObject &&
218
+ x.entries.every(([p, ps]) => admitsAnyValue(cx, ps, child(props, p)))));
219
+ const req = s.get("required");
220
+ return allowed && Array.isArray(req) && req.length > 0
221
+ ? req.filter((n) => typeof n === "string")
222
+ : undefined;
223
+ }
224
+ /** Whether a schema admits every value: `true`, annotations alone, or a `$ref` to such a schema next to them. */
225
+ function admitsAnyValue(cx, s, at) {
226
+ if (s === true)
227
+ return true;
228
+ if (!(s instanceof JsonObject))
229
+ return false;
230
+ if (!s.entries.every(([k]) => k === "$ref" || isAnnotation(k)))
231
+ return false;
232
+ const r = s.str("$ref");
233
+ return (r === undefined ||
234
+ follow(cx, r, at, (t, where) => admitsAnyValue(cx, t, where)));
235
+ }
236
+ /** `f` on the schema a `$ref` reaches, with the base URI of the document it is in. */
237
+ function follow(cx, r, at, f) {
238
+ const { value, base, where } = cx.locate(r, at);
239
+ return cx.withBase(base, () => f(value, where));
240
+ }
144
241
  /** Keywords that only apply to objects; their presence implies `type: object`. */
145
242
  function hasObjectKeywords(v) {
146
243
  return [
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kanagata",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "TypeScript types from OpenAPI 3.0 / 3.1 / 3.2 (JSON or YAML), and a typed fetch client",
5
5
  "keywords": [
6
6
  "openapi",