@jinshuju/cli 0.20.0 → 0.22.0
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.
- package/README.md +45 -3
- package/dist/cli.js +5 -2
- package/dist/commands/field.js +14 -2
- package/dist/commands/form.js +21 -4
- package/dist/commands/index.js +6 -0
- package/dist/commands/message.d.ts +2 -0
- package/dist/commands/message.js +134 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.js +17 -0
- package/dist/generated/operations.d.ts +3 -1
- package/dist/generated/operations.js +616 -29
- package/dist/http.d.ts +6 -0
- package/dist/http.js +58 -11
- package/dist/options.js +116 -4
- package/dist/preflight.d.ts +32 -0
- package/dist/preflight.js +156 -0
- package/dist/schema.d.ts +39 -0
- package/dist/schema.js +239 -0
- package/dist/spec.d.ts +19 -0
- package/dist/spec.js +23 -0
- package/package.json +2 -2
package/dist/http.d.ts
CHANGED
|
@@ -17,6 +17,12 @@ export type HttpRequest = {
|
|
|
17
17
|
};
|
|
18
18
|
export interface HttpClient {
|
|
19
19
|
request<T>(request: HttpRequest): Promise<T>;
|
|
20
|
+
/**
|
|
21
|
+
* What `request` would be refused for before it was sent, thrown now and
|
|
22
|
+
* nothing sent: a command with two requests to make asks about the second
|
|
23
|
+
* before the first has written. A client that sends as given has none.
|
|
24
|
+
*/
|
|
25
|
+
check?(request: HttpRequest): void;
|
|
20
26
|
}
|
|
21
27
|
/**
|
|
22
28
|
* The path with its query string. A list value repeats its parameter as
|
package/dist/http.js
CHANGED
|
@@ -77,8 +77,12 @@ function describeFailure(response, text, parsed) {
|
|
|
77
77
|
return status;
|
|
78
78
|
const body = parsed.value;
|
|
79
79
|
const stated = body.message ?? body.error_description;
|
|
80
|
-
if (typeof stated === 'string' && stated.trim())
|
|
81
|
-
|
|
80
|
+
if (typeof stated === 'string' && stated.trim()) {
|
|
81
|
+
const reasons = everyReason(body, stated);
|
|
82
|
+
if (!reasons)
|
|
83
|
+
return withCode(stated, body.error);
|
|
84
|
+
return [withCode(reasons.head, body.error), ...reasons.rest.map((line) => ` ${line}`)].join('\n');
|
|
85
|
+
}
|
|
82
86
|
return rowErrors(body) ?? `${status}: ${snippet(JSON.stringify(parsed.value))}`;
|
|
83
87
|
}
|
|
84
88
|
function withCode(reason, code) {
|
|
@@ -86,21 +90,64 @@ function withCode(reason, code) {
|
|
|
86
90
|
return reason;
|
|
87
91
|
return `${reason} (${code})`;
|
|
88
92
|
}
|
|
93
|
+
/** The most reasons worth putting in front of a reader; `--output json` carries them all. */
|
|
94
|
+
const MAX_REASONS = 20;
|
|
89
95
|
/**
|
|
90
|
-
* A refusal with several reasons
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
96
|
+
* A refusal with several reasons, each of them said.
|
|
97
|
+
*
|
|
98
|
+
* The server states a reason in error_description and lists every reason in
|
|
99
|
+
* errors[]. The description repeats only the first: a routine against two of
|
|
100
|
+
* the page's rules states one of them, and a request body validation states
|
|
101
|
+
* three of what may be a hundred and counts the rest. A caller shown the
|
|
102
|
+
* description alone corrects what it was told, sends again, and learns the
|
|
103
|
+
* next. So every reason is a line of its own, under whatever the description
|
|
104
|
+
* said ahead of the first ("Request body invalid:"). One line per pointer,
|
|
105
|
+
* carrying each distinct reason said at it: a value may break two rules at
|
|
106
|
+
* once (too short, and not of its pattern), and a field that may be one of
|
|
107
|
+
* several shapes is told about the same pointer once per shape. A list too
|
|
108
|
+
* long to read is cut, and says how many it kept back.
|
|
95
109
|
*/
|
|
96
110
|
function everyReason(body, stated) {
|
|
97
111
|
const errors = body.errors;
|
|
98
|
-
if (!Array.isArray(errors) || errors.length
|
|
112
|
+
if (!Array.isArray(errors) || errors.length === 0)
|
|
113
|
+
return undefined;
|
|
114
|
+
// Pointer to the reasons said at it, in the order the pointers were first named.
|
|
115
|
+
const said = new Map();
|
|
116
|
+
let first;
|
|
117
|
+
let count = 0;
|
|
118
|
+
for (const item of errors) {
|
|
119
|
+
if (item === null || typeof item !== 'object')
|
|
120
|
+
continue;
|
|
121
|
+
const { pointer, message } = item;
|
|
122
|
+
if (typeof message !== 'string' || !message.trim())
|
|
123
|
+
continue;
|
|
124
|
+
const at = typeof pointer === 'string' && pointer ? pointer : undefined;
|
|
125
|
+
const entry = said.get(at ?? message) ?? { at, messages: [] };
|
|
126
|
+
if (entry.messages.includes(message))
|
|
127
|
+
continue;
|
|
128
|
+
entry.messages.push(message);
|
|
129
|
+
said.set(at ?? message, entry);
|
|
130
|
+
first ??= message;
|
|
131
|
+
count += 1;
|
|
132
|
+
}
|
|
133
|
+
if (first === undefined)
|
|
99
134
|
return undefined;
|
|
100
|
-
|
|
101
|
-
if (messages[0] !== stated || !messages.every((message) => typeof message === 'string'))
|
|
135
|
+
if (count === 1 && stated.includes(first))
|
|
102
136
|
return undefined;
|
|
103
|
-
|
|
137
|
+
const lines = [...said.values()].map(({ at, messages }) => {
|
|
138
|
+
const text = messages.join('; ');
|
|
139
|
+
return at && !messages.some((message) => message.includes(at)) ? `${at}: ${text}` : text;
|
|
140
|
+
});
|
|
141
|
+
const kept = lines.slice(0, MAX_REASONS);
|
|
142
|
+
if (lines.length > kept.length)
|
|
143
|
+
kept.push(`… and ${lines.length - kept.length} more; --output json carries them all`);
|
|
144
|
+
const index = stated.indexOf(first);
|
|
145
|
+
// The description's own words introduce the list; a description that does not
|
|
146
|
+
// repeat the first reason is one more thing said, so it stays whole.
|
|
147
|
+
const heading = index < 0 ? stated : stated.slice(0, index).trim();
|
|
148
|
+
if (heading)
|
|
149
|
+
return { head: heading, rest: kept };
|
|
150
|
+
return { head: kept[0], rest: kept.slice(1) };
|
|
104
151
|
}
|
|
105
152
|
/**
|
|
106
153
|
* A batch write answers per row: `{errors: [{index, reason}]}`. The index is the
|
package/dist/options.js
CHANGED
|
@@ -12,6 +12,11 @@ export const GLOBAL_OPTIONS = [
|
|
|
12
12
|
description: 'Output format: text, json, or jsonl (one JSON object per line; a listing streams its rows)'
|
|
13
13
|
},
|
|
14
14
|
{ name: '--config', type: 'string', placeholder: '<path>', description: 'Config file path' },
|
|
15
|
+
{
|
|
16
|
+
name: '--as-is',
|
|
17
|
+
type: 'boolean',
|
|
18
|
+
description: 'Send a payload as it is, skipping the check against the openapi.yaml this CLI shipped with; for when the API has since learned what it refuses'
|
|
19
|
+
},
|
|
15
20
|
{ name: '--help', short: '-h', type: 'boolean', description: 'Show help' },
|
|
16
21
|
// -V rather than -v: nearly every CLI reads -v as verbose, and a flag that
|
|
17
22
|
// prints a version where the reader expected more output is a small betrayal.
|
|
@@ -91,12 +96,58 @@ export const CONTAINER_LIST_OPTIONS = [
|
|
|
91
96
|
* which the spec does not list.
|
|
92
97
|
*/
|
|
93
98
|
const FILTER_OPERATORS = choices('listEntries', 'filters[].operator');
|
|
99
|
+
/**
|
|
100
|
+
* What a caller writes for an operator the API spells otherwise — `=`, `in`,
|
|
101
|
+
* `not_empty` — and the server answers with the whole list; the one meant is
|
|
102
|
+
* named instead.
|
|
103
|
+
*/
|
|
104
|
+
const OPERATOR_ALIASES = {
|
|
105
|
+
'=': 'eq',
|
|
106
|
+
'==': 'eq',
|
|
107
|
+
'===': 'eq',
|
|
108
|
+
is: 'eq',
|
|
109
|
+
equals: 'eq',
|
|
110
|
+
'!=': 'ne',
|
|
111
|
+
'!==': 'ne',
|
|
112
|
+
'<>': 'ne',
|
|
113
|
+
neq: 'ne',
|
|
114
|
+
not: 'ne',
|
|
115
|
+
'>': 'gt',
|
|
116
|
+
'>=': 'gte',
|
|
117
|
+
'<': 'lt',
|
|
118
|
+
'<=': 'lte',
|
|
119
|
+
in: 'any_in',
|
|
120
|
+
not_in: 'none_in',
|
|
121
|
+
nin: 'none_in',
|
|
122
|
+
not_empty: 'not_null',
|
|
123
|
+
not_blank: 'not_null',
|
|
124
|
+
present: 'not_null',
|
|
125
|
+
exists: 'not_null',
|
|
126
|
+
is_not_null: 'not_null',
|
|
127
|
+
empty: 'null',
|
|
128
|
+
blank: 'null',
|
|
129
|
+
is_null: 'null',
|
|
130
|
+
missing: 'null',
|
|
131
|
+
contains: 'like',
|
|
132
|
+
includes: 'like',
|
|
133
|
+
match: 'like',
|
|
134
|
+
matches: 'like',
|
|
135
|
+
not_contains: 'not_like',
|
|
136
|
+
excludes: 'not_like',
|
|
137
|
+
starts_with: 'start',
|
|
138
|
+
startswith: 'start',
|
|
139
|
+
begins_with: 'start',
|
|
140
|
+
ends_with: 'end',
|
|
141
|
+
endswith: 'end',
|
|
142
|
+
last: 'within_last',
|
|
143
|
+
within: 'within_last'
|
|
144
|
+
};
|
|
94
145
|
export const FILTER_OPTION = {
|
|
95
146
|
name: '--filter',
|
|
96
147
|
type: 'string',
|
|
97
148
|
repeatable: true,
|
|
98
149
|
placeholder: "'<field> <op> [value]'",
|
|
99
|
-
description: 'Filter condition, repeatable, AND-combined. ' +
|
|
150
|
+
description: 'Filter condition, one per flag, repeatable, AND-combined. ' +
|
|
100
151
|
(FILTER_OPERATORS ? `Operators: ${FILTER_OPERATORS.join(' ')}. ` : '') +
|
|
101
152
|
"e.g. 'field_3 gte 80', 'created_at within_last 30d', 'field_4 between 1,10'"
|
|
102
153
|
};
|
|
@@ -165,6 +216,14 @@ export function parseFilter(input) {
|
|
|
165
216
|
}
|
|
166
217
|
const [, field, operator, rest] = match;
|
|
167
218
|
const raw = rest.trim();
|
|
219
|
+
checkOperator(operator);
|
|
220
|
+
checkOneCondition(field, operator, raw);
|
|
221
|
+
// An operator this copy of the spec does not list goes as written, its
|
|
222
|
+
// value as the one string it was: the API that learned it says what shape
|
|
223
|
+
// the value takes, and whether it is one at all.
|
|
224
|
+
if (FILTER_OPERATORS && !FILTER_OPERATORS.includes(operator)) {
|
|
225
|
+
return raw ? { field, operator, value: unquoted(raw) } : { field, operator };
|
|
226
|
+
}
|
|
168
227
|
if (NO_VALUE_OPERATORS.has(operator)) {
|
|
169
228
|
if (raw)
|
|
170
229
|
throw new UsageError(`--filter operator '${operator}' takes no value, got ${JSON.stringify(raw)}`);
|
|
@@ -183,7 +242,49 @@ export function parseFilter(input) {
|
|
|
183
242
|
return { field, operator, value: splitList(raw) };
|
|
184
243
|
if (operator === 'within_last')
|
|
185
244
|
return { field, operator, value: parseRelativeWindow(raw) };
|
|
186
|
-
return { field, operator, value: raw };
|
|
245
|
+
return { field, operator, value: unquoted(raw) };
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* An operator spelled as the API does not spell it — `=`, `in`, `not_empty`,
|
|
249
|
+
* `EQ` — is refused here with the one meant. One the spec's list does not
|
|
250
|
+
* hold and no alias names goes through: the list is this copy's, and the API
|
|
251
|
+
* may have learned the operator since, so the API is the one to refuse it.
|
|
252
|
+
*/
|
|
253
|
+
function checkOperator(operator) {
|
|
254
|
+
if (!FILTER_OPERATORS || FILTER_OPERATORS.includes(operator))
|
|
255
|
+
return;
|
|
256
|
+
const lowered = operator.toLowerCase();
|
|
257
|
+
const meant = FILTER_OPERATORS.includes(lowered) ? lowered : OPERATOR_ALIASES[lowered];
|
|
258
|
+
if (meant === undefined)
|
|
259
|
+
return;
|
|
260
|
+
throw new UsageError(`--filter operator '${operator}' is written '${meant}'. Operators: ${FILTER_OPERATORS.join(' ')}`);
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Two conditions in one --filter — `field_15 eq S5mp and field_4 equal 通过`
|
|
264
|
+
* — would go as one, the whole tail being field_15's value, and the server
|
|
265
|
+
* would say the value is not one of its choices. A value may well contain
|
|
266
|
+
* "and": one written in quotes is the whole value, and an "and" is read as
|
|
267
|
+
* a second condition only when a field and an operator as the API spells it
|
|
268
|
+
* (or a symbol, `=`) follow, never on a word like "is" or "in" that may be
|
|
269
|
+
* the value's own. `&&` is nobody's text, so an alias counts after it. The
|
|
270
|
+
* refusal says how to send the value whole, for the case it was one.
|
|
271
|
+
*/
|
|
272
|
+
function checkOneCondition(field, operator, raw) {
|
|
273
|
+
if (raw.startsWith('"') || raw.startsWith("'"))
|
|
274
|
+
return;
|
|
275
|
+
const next = /\s+(and|&&)\s+(\S+)\s+(\S+)/i.exec(raw);
|
|
276
|
+
if (!next)
|
|
277
|
+
return;
|
|
278
|
+
const [, joiner, nextField, nextOperator] = next;
|
|
279
|
+
const alias = OPERATOR_ALIASES[nextOperator.toLowerCase()];
|
|
280
|
+
const spelled = FILTER_OPERATORS?.includes(nextOperator) || /^[=!<>]+$/.test(nextOperator);
|
|
281
|
+
if (!spelled && !(joiner === '&&' && alias !== undefined))
|
|
282
|
+
return;
|
|
283
|
+
const first = raw.slice(0, next.index).trim();
|
|
284
|
+
const quote = raw.includes('"') ? "'" : '"';
|
|
285
|
+
throw new UsageError(`--filter takes one condition; write the next as its own flag: ` +
|
|
286
|
+
`--filter '${field} ${operator} ${first}' --filter '${nextField} ${alias ?? nextOperator} …'. ` +
|
|
287
|
+
`If that is the value, quote it: --filter '${field} ${operator} ${quote}${raw}${quote}'`);
|
|
187
288
|
}
|
|
188
289
|
/** `30d` into `{unit: 'day', n: 30}`. */
|
|
189
290
|
function parseRelativeWindow(raw) {
|
|
@@ -196,13 +297,24 @@ function parseRelativeWindow(raw) {
|
|
|
196
297
|
throw new UsageError(`--filter within_last needs a positive window, got ${JSON.stringify(raw)}`);
|
|
197
298
|
return { unit: RELATIVE_UNITS[match[2].toLowerCase()], n };
|
|
198
299
|
}
|
|
199
|
-
/** A comma-separated list, with backslash-escaped commas kept as text. */
|
|
300
|
+
/** A comma-separated list, with backslash-escaped commas kept as text, and each value out of its quotes. */
|
|
200
301
|
function splitList(raw) {
|
|
201
302
|
return raw
|
|
202
303
|
.split(/(?<!\\),/)
|
|
203
|
-
.map((part) => part.replace(/\\,/g, ',').trim())
|
|
304
|
+
.map((part) => unquoted(part.replace(/\\,/g, ',').trim()))
|
|
204
305
|
.filter((part) => part.length > 0);
|
|
205
306
|
}
|
|
307
|
+
/**
|
|
308
|
+
* A value written in quotes — `between "2026-10-01 00:00:00","2026-10-07
|
|
309
|
+
* 23:59:59"`, as a shell user quotes out of habit — went up with the quotes
|
|
310
|
+
* on, and the server said it was not a date range. A pair of matching quotes
|
|
311
|
+
* around the whole value is taken off; a quote inside one is text.
|
|
312
|
+
*/
|
|
313
|
+
function unquoted(text) {
|
|
314
|
+
const quote = text[0];
|
|
315
|
+
const paired = text.length >= 2 && (quote === '"' || quote === "'") && text.endsWith(quote);
|
|
316
|
+
return paired ? text.slice(1, -1) : text;
|
|
317
|
+
}
|
|
206
318
|
/** `created_at:desc`; the order defaults to asc, as a bare field reads. */
|
|
207
319
|
export function parseSort(input) {
|
|
208
320
|
const [field, order = 'asc'] = input.split(':');
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type Refusal } from './errors.js';
|
|
2
|
+
import type { HttpClient, HttpRequest } from './http.js';
|
|
3
|
+
/**
|
|
4
|
+
* The one look the CLI takes at a body before sending it.
|
|
5
|
+
*
|
|
6
|
+
* API v1 validates every request against the openapi.yaml it runs, the only
|
|
7
|
+
* copy never out of date, and its 400 says what is wrong. An installed CLI
|
|
8
|
+
* keeps the spec it shipped with until someone upgrades it, so a check here
|
|
9
|
+
* against that copy would refuse, from then on, each field type, key and
|
|
10
|
+
* value the API learns: an old client would be stricter than the API it
|
|
11
|
+
* talks to. So the look is a narrow one. It refuses what no newer spec could
|
|
12
|
+
* have made right — a key that is required, a value of the wrong type, null
|
|
13
|
+
* where nothing may be null, a field read back and written back as it came —
|
|
14
|
+
* and sends everything else for the API to judge: a key or a value this copy
|
|
15
|
+
* does not list may be one the API has learned since. What the CLI's callers
|
|
16
|
+
* were most often refused for was the first kind: a field's `validation`
|
|
17
|
+
* written back as `form get` returned it, `null` for every key left unset, a
|
|
18
|
+
* choice to update named by its value rather than its api_code.
|
|
19
|
+
*
|
|
20
|
+
* What the look finds is said at once, every reason at its pointer, and the
|
|
21
|
+
* request is not sent. `--as-is` sends the body anyway, for when the API has
|
|
22
|
+
* in fact learned what this copy refuses; the refusal names the day the copy
|
|
23
|
+
* was fetched so that case can be told.
|
|
24
|
+
*/
|
|
25
|
+
/**
|
|
26
|
+
* The client with the look on the way: a request whose body it refuses never
|
|
27
|
+
* reaches the one wrapped. Its `check` is the same look without the sending,
|
|
28
|
+
* for a command that has a second request ready before its first has written.
|
|
29
|
+
*/
|
|
30
|
+
export declare function checked(client: HttpClient, asIs: boolean): HttpClient;
|
|
31
|
+
/** What a request's body is refused for, each at its pointer; nothing when it may go. */
|
|
32
|
+
export declare function preflight(request: HttpRequest): Refusal[];
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { PreflightError } from './errors.js';
|
|
2
|
+
import { OPERATIONS, SPEC_DATE } from './generated/operations.js';
|
|
3
|
+
import { validate } from './schema.js';
|
|
4
|
+
import { operationOf } from './spec.js';
|
|
5
|
+
import { isRecord } from './values.js';
|
|
6
|
+
/**
|
|
7
|
+
* The one look the CLI takes at a body before sending it.
|
|
8
|
+
*
|
|
9
|
+
* API v1 validates every request against the openapi.yaml it runs, the only
|
|
10
|
+
* copy never out of date, and its 400 says what is wrong. An installed CLI
|
|
11
|
+
* keeps the spec it shipped with until someone upgrades it, so a check here
|
|
12
|
+
* against that copy would refuse, from then on, each field type, key and
|
|
13
|
+
* value the API learns: an old client would be stricter than the API it
|
|
14
|
+
* talks to. So the look is a narrow one. It refuses what no newer spec could
|
|
15
|
+
* have made right — a key that is required, a value of the wrong type, null
|
|
16
|
+
* where nothing may be null, a field read back and written back as it came —
|
|
17
|
+
* and sends everything else for the API to judge: a key or a value this copy
|
|
18
|
+
* does not list may be one the API has learned since. What the CLI's callers
|
|
19
|
+
* were most often refused for was the first kind: a field's `validation`
|
|
20
|
+
* written back as `form get` returned it, `null` for every key left unset, a
|
|
21
|
+
* choice to update named by its value rather than its api_code.
|
|
22
|
+
*
|
|
23
|
+
* What the look finds is said at once, every reason at its pointer, and the
|
|
24
|
+
* request is not sent. `--as-is` sends the body anyway, for when the API has
|
|
25
|
+
* in fact learned what this copy refuses; the refusal names the day the copy
|
|
26
|
+
* was fetched so that case can be told.
|
|
27
|
+
*/
|
|
28
|
+
/**
|
|
29
|
+
* The client with the look on the way: a request whose body it refuses never
|
|
30
|
+
* reaches the one wrapped. Its `check` is the same look without the sending,
|
|
31
|
+
* for a command that has a second request ready before its first has written.
|
|
32
|
+
*/
|
|
33
|
+
export function checked(client, asIs) {
|
|
34
|
+
if (asIs)
|
|
35
|
+
return client;
|
|
36
|
+
const check = (request) => {
|
|
37
|
+
const refusals = preflight(request);
|
|
38
|
+
if (refusals.length > 0)
|
|
39
|
+
throw new PreflightError(describe(refusals), refusals);
|
|
40
|
+
};
|
|
41
|
+
return {
|
|
42
|
+
check,
|
|
43
|
+
async request(request) {
|
|
44
|
+
check(request);
|
|
45
|
+
return client.request(request);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
/** What a request's body is refused for, each at its pointer; nothing when it may go. */
|
|
50
|
+
export function preflight(request) {
|
|
51
|
+
const id = operationOf(request);
|
|
52
|
+
if (id === undefined || request.body === undefined)
|
|
53
|
+
return [];
|
|
54
|
+
const schema = OPERATIONS[id].body?.content['application/json'];
|
|
55
|
+
if (schema === undefined)
|
|
56
|
+
return [];
|
|
57
|
+
// What the server reads is the JSON, where an undefined key is no key at all.
|
|
58
|
+
const body = JSON.parse(JSON.stringify(request.body));
|
|
59
|
+
const refusals = [];
|
|
60
|
+
const at = new Set();
|
|
61
|
+
const refuse = (refusal) => {
|
|
62
|
+
if (refusal === undefined || at.has(refusal.pointer))
|
|
63
|
+
return;
|
|
64
|
+
at.add(refusal.pointer);
|
|
65
|
+
refusals.push(refusal);
|
|
66
|
+
};
|
|
67
|
+
for (const issue of validate(schema, body))
|
|
68
|
+
refuse(refusalOf(issue, request.method));
|
|
69
|
+
for (const refusal of ownRules(id, body))
|
|
70
|
+
refuse(refusal);
|
|
71
|
+
return refusals;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* The issues a newer spec could not have made right, as refusals; the rest
|
|
75
|
+
* as nothing. A key the schema does not list, a value outside its list, a
|
|
76
|
+
* range or a length may all have grown since this copy was fetched, and a
|
|
77
|
+
* shape guessed at (anyOf, oneOf) may have been the wrong guess. A required
|
|
78
|
+
* key, a type and a null are not that kind of thing.
|
|
79
|
+
*/
|
|
80
|
+
function refusalOf(issue, method) {
|
|
81
|
+
if (!issue.certain)
|
|
82
|
+
return undefined;
|
|
83
|
+
const { pointer, kind, value } = issue;
|
|
84
|
+
if (kind === 'required')
|
|
85
|
+
return { pointer, message: `${issue.message}${hint(pointer)}` };
|
|
86
|
+
if (kind === 'type') {
|
|
87
|
+
if (value === null) {
|
|
88
|
+
const instead = method === 'PATCH' || method === 'PUT' ? 'to leave it unchanged' : 'instead';
|
|
89
|
+
return { pointer, message: `is null; leave the key out ${instead}` };
|
|
90
|
+
}
|
|
91
|
+
return { pointer, message: `${issue.message}, got ${JSON.stringify(value)}` };
|
|
92
|
+
}
|
|
93
|
+
if (kind === 'unknown_key' && lastSegment(pointer) === 'validation' && isRecord(value)) {
|
|
94
|
+
return { pointer, message: flattened(value, issue.takes ?? []) };
|
|
95
|
+
}
|
|
96
|
+
return undefined;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* `validation` is how a field reads: `form get` and `field list` answer
|
|
100
|
+
* `{validation: {required, unique, minimum_length, …}}`, and a field written
|
|
101
|
+
* back as it came carries it. The write takes the same keys flat on the
|
|
102
|
+
* field, so the message says the payload that would do, worked out from the
|
|
103
|
+
* keys the write does take rather than from a list kept here.
|
|
104
|
+
*/
|
|
105
|
+
function flattened(validation, takes) {
|
|
106
|
+
const flat = Object.fromEntries(Object.entries(validation).filter(([key]) => takes.includes(key)));
|
|
107
|
+
const dropped = Object.keys(validation).filter((key) => !takes.includes(key));
|
|
108
|
+
const parts = ['validation is how a field reads, not how it is written'];
|
|
109
|
+
if (Object.keys(flat).length > 0)
|
|
110
|
+
parts.push(`write its keys flat on the field: ${JSON.stringify(flat)}`);
|
|
111
|
+
if (dropped.length > 0)
|
|
112
|
+
parts.push(`leave out ${dropped.join(', ')}, which a write does not take`);
|
|
113
|
+
return parts.join('; ');
|
|
114
|
+
}
|
|
115
|
+
/** What to add to a refusal at this pointer, when the spec's own words would leave the caller guessing. */
|
|
116
|
+
function hint(pointer) {
|
|
117
|
+
if (/\/update_choices\/\d+\/update\/\d+\/api_code$/.test(pointer)) {
|
|
118
|
+
return '; a choice to update is named by its api_code, which `jinshuju field list` shows';
|
|
119
|
+
}
|
|
120
|
+
return '';
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* What the API refuses by a rule of its own rather than by the schema — kept
|
|
124
|
+
* here only where the rule reads off the payload alone. `choices` on a field
|
|
125
|
+
* update is refused by what the field is now, a CascadeDropDown or a text
|
|
126
|
+
* field turning into a choice field taking them; that is the server's to
|
|
127
|
+
* know, so it is the server's to refuse.
|
|
128
|
+
*/
|
|
129
|
+
function ownRules(id, body) {
|
|
130
|
+
const refusals = [];
|
|
131
|
+
if (id === 'createForm' && isRecord(body) && Array.isArray(body.fields)) {
|
|
132
|
+
const first = body.fields[0];
|
|
133
|
+
const typeOf = (field) => (isRecord(field) ? field.type : undefined);
|
|
134
|
+
if (body.fields.some((field) => typeOf(field) === 'PageBreak') && typeOf(first) !== 'PageBreak') {
|
|
135
|
+
refusals.push({
|
|
136
|
+
pointer: '/fields/0',
|
|
137
|
+
message: 'a form with page breaks starts with one; put a PageBreak first'
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return refusals;
|
|
142
|
+
}
|
|
143
|
+
function describe(refusals) {
|
|
144
|
+
const copy = SPEC_DATE === undefined ? 'the openapi.yaml' : `the openapi.yaml of ${SPEC_DATE}`;
|
|
145
|
+
return [
|
|
146
|
+
`the payload would be refused, by ${copy} this CLI shipped with:`,
|
|
147
|
+
...refusals.map(({ pointer, message }) => ` ${pointer || '/'}: ${message}`),
|
|
148
|
+
'The request was not sent. --as-is sends it anyway, for the API to judge by the spec it runs.'
|
|
149
|
+
].join('\n');
|
|
150
|
+
}
|
|
151
|
+
function lastSegment(pointer) {
|
|
152
|
+
return pointer
|
|
153
|
+
.slice(pointer.lastIndexOf('/') + 1)
|
|
154
|
+
.replace(/~1/g, '/')
|
|
155
|
+
.replace(/~0/g, '~');
|
|
156
|
+
}
|
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Schema } from './spec.js';
|
|
2
|
+
/**
|
|
3
|
+
* A value held to a schema, as scripts/generate.mjs keeps one. The keywords
|
|
4
|
+
* the generator keeps are exactly the ones checked here, and it stops on one
|
|
5
|
+
* it does not know, so nothing in openapi.yaml is passed over unnoticed.
|
|
6
|
+
*
|
|
7
|
+
* Each issue says which kind of rule the value broke, and whether that rule
|
|
8
|
+
* was certainly the one that applied. A value that may be one of several
|
|
9
|
+
* shapes (oneOf, anyOf) is told about the shape it came closest to, and that
|
|
10
|
+
* is a guess, so nothing found under it is certain. A discriminator's branch
|
|
11
|
+
* is no guess — the value's own `type` named it — and what is found under it
|
|
12
|
+
* is as certain as anything found directly.
|
|
13
|
+
*/
|
|
14
|
+
export type IssueKind =
|
|
15
|
+
/** Not of the type the schema says: a string where a number goes, null where nothing may be. */
|
|
16
|
+
'type'
|
|
17
|
+
/** A key the schema requires is missing. */
|
|
18
|
+
| 'required'
|
|
19
|
+
/** Outside the list the schema gives, or not the one constant it names. */
|
|
20
|
+
| 'enum'
|
|
21
|
+
/** A key on an object that takes only the keys it names. */
|
|
22
|
+
| 'unknown_key'
|
|
23
|
+
/** Outside a range or a length, or not matching a pattern. */
|
|
24
|
+
| 'range'
|
|
25
|
+
/** Fits none of several shapes, more than one of them, or one it must not. */
|
|
26
|
+
| 'shape';
|
|
27
|
+
export interface Issue {
|
|
28
|
+
/** JSON Pointer to the value, `` for the value itself. */
|
|
29
|
+
readonly pointer: string;
|
|
30
|
+
readonly kind: IssueKind;
|
|
31
|
+
readonly message: string;
|
|
32
|
+
readonly value: unknown;
|
|
33
|
+
/** Whether the rule broken was certainly the one that applied, and not the closest of several shapes. */
|
|
34
|
+
readonly certain: boolean;
|
|
35
|
+
/** For an unknown key, the keys the object does take. */
|
|
36
|
+
readonly takes?: readonly string[];
|
|
37
|
+
}
|
|
38
|
+
/** The ways a value breaks a schema, first found first. */
|
|
39
|
+
export declare function validate(schema: Schema | boolean, value: unknown, pointer?: string, certain?: boolean): Issue[];
|