@crawlee/utils 4.0.0-beta.164 → 4.0.0-beta.166
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/index.d.ts +1 -1
- package/index.js +1 -1
- package/internals/validation.d.ts +1 -25
- package/internals/validation.js +1 -140
- package/package.json +6 -5
package/index.d.ts
CHANGED
|
@@ -8,4 +8,4 @@ export * from './internals/extract-microdata.js';
|
|
|
8
8
|
export * from './internals/open_graph_parser.js';
|
|
9
9
|
export * from './internals/robots.js';
|
|
10
10
|
export * from './internals/sitemap.js';
|
|
11
|
-
export
|
|
11
|
+
export { ArgumentValidationError } from './internals/validation.js';
|
package/index.js
CHANGED
|
@@ -7,4 +7,4 @@ export * from './internals/extract-microdata.js';
|
|
|
7
7
|
export * from './internals/open_graph_parser.js';
|
|
8
8
|
export * from './internals/robots.js';
|
|
9
9
|
export * from './internals/sitemap.js';
|
|
10
|
-
export
|
|
10
|
+
export { ArgumentValidationError } from './internals/validation.js';
|
|
@@ -1,25 +1 @@
|
|
|
1
|
-
|
|
2
|
-
/**
|
|
3
|
-
* Thrown when an argument fails schema validation.
|
|
4
|
-
*
|
|
5
|
-
* Its `message` is a human-readable sentence naming the offending field and the
|
|
6
|
-
* value it received (rather than a raw JSON dump). The structured
|
|
7
|
-
* {@link https://zod.dev | zod} issues are available on `issues`, and the
|
|
8
|
-
* original `ZodError` on `cause`, for programmatic inspection.
|
|
9
|
-
*/
|
|
10
|
-
export declare class ArgumentValidationError extends Error {
|
|
11
|
-
/** Structured issues from the underlying schema check. */
|
|
12
|
-
readonly issues: z.ZodError['issues'];
|
|
13
|
-
/** The raw zod error that triggered this. */
|
|
14
|
-
readonly cause: z.ZodError;
|
|
15
|
-
constructor(error: z.ZodError, value: unknown, label?: string);
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Parses `value` with `schema`, returning the typed result (with schema defaults applied).
|
|
19
|
-
* Throws {@link ArgumentValidationError} on failure.
|
|
20
|
-
*
|
|
21
|
-
* The optional `label` names the interface being validated and is appended to every error line
|
|
22
|
-
* (e.g. ``… at `maxRequestRetries` in `BasicCrawlerOptions` ``).
|
|
23
|
-
* @internal
|
|
24
|
-
*/
|
|
25
|
-
export declare function parseArgument<TValue, TSchema extends z.ZodType>(value: TValue, schema: TSchema, label?: string): TValue & z.output<TSchema>;
|
|
1
|
+
export { ArgumentValidationError, parseArgument } from '@apify/validations';
|
package/internals/validation.js
CHANGED
|
@@ -1,140 +1 @@
|
|
|
1
|
-
|
|
2
|
-
function formatIssuePath(path) {
|
|
3
|
-
let out = '';
|
|
4
|
-
for (const key of path) {
|
|
5
|
-
if (typeof key === 'number')
|
|
6
|
-
out += `[${key}]`;
|
|
7
|
-
else
|
|
8
|
-
out += out ? `.${String(key)}` : String(key);
|
|
9
|
-
}
|
|
10
|
-
return out;
|
|
11
|
-
}
|
|
12
|
-
/** Reads the value at `path` from the validated input, to include in the error. */
|
|
13
|
-
function valueAtPath(root, path) {
|
|
14
|
-
let current = root;
|
|
15
|
-
for (const key of path) {
|
|
16
|
-
if (current === null || typeof current !== 'object')
|
|
17
|
-
return undefined;
|
|
18
|
-
current = current[key];
|
|
19
|
-
}
|
|
20
|
-
return current;
|
|
21
|
-
}
|
|
22
|
-
/** Names the runtime type of `value` the way zod's own messages do (`null`, `array`, `string`, …). */
|
|
23
|
-
function describeType(value) {
|
|
24
|
-
if (value === null)
|
|
25
|
-
return 'null';
|
|
26
|
-
if (Array.isArray(value))
|
|
27
|
-
return 'array';
|
|
28
|
-
return typeof value;
|
|
29
|
-
}
|
|
30
|
-
/** The bare custom-schema messages that stop at the expected type, e.g. `Invalid input: expected number`. */
|
|
31
|
-
const BARE_EXPECTED_TYPE_MESSAGE = /^Invalid input: expected (an array of .+|a typed array|an object|object|array|function|number|string|boolean)$/;
|
|
32
|
-
/** Longest received string rendered in an error; the rest is elided. */
|
|
33
|
-
const MAX_RENDERED_STRING_LENGTH = 200;
|
|
34
|
-
/** Renders a primitive received value for an error; skips objects/Dates (noisy). */
|
|
35
|
-
function describeReceived(value) {
|
|
36
|
-
switch (typeof value) {
|
|
37
|
-
case 'string':
|
|
38
|
-
// An empty string would render as bare backticks — make it visible.
|
|
39
|
-
if (value === '')
|
|
40
|
-
return "''";
|
|
41
|
-
return value.length > MAX_RENDERED_STRING_LENGTH
|
|
42
|
-
? `${value.slice(0, MAX_RENDERED_STRING_LENGTH)}… (${value.length - MAX_RENDERED_STRING_LENGTH} more characters)`
|
|
43
|
-
: value;
|
|
44
|
-
case 'number':
|
|
45
|
-
case 'boolean':
|
|
46
|
-
case 'bigint':
|
|
47
|
-
return String(value);
|
|
48
|
-
default:
|
|
49
|
-
return undefined;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
/** Renders the received side of a sentence: ``received the string `abc` ``, `received NaN`, `received array`. */
|
|
53
|
-
function describeReceivedClause(value) {
|
|
54
|
-
if (typeof value === 'number' && Number.isNaN(value))
|
|
55
|
-
return 'received NaN';
|
|
56
|
-
if (value === '')
|
|
57
|
-
return 'received an empty string';
|
|
58
|
-
const rendered = describeReceived(value);
|
|
59
|
-
return rendered === undefined
|
|
60
|
-
? `received ${describeType(value)}`
|
|
61
|
-
: `received the ${describeType(value)} \`${rendered}\``;
|
|
62
|
-
}
|
|
63
|
-
/** Renders one issue as a line each; a union expands into a line per failed arm. */
|
|
64
|
-
function formatIssue(issue, root, basePath) {
|
|
65
|
-
const path = [...basePath, ...issue.path];
|
|
66
|
-
// A union's own message is a bare "Invalid input" — the useful part is in `errors`,
|
|
67
|
-
// whose paths are relative to the union, hence passing `path` down as the base.
|
|
68
|
-
if (issue.code === 'invalid_union') {
|
|
69
|
-
return issue.errors.flatMap((arm) => arm.flatMap((nested) => formatIssue(nested, root, path)));
|
|
70
|
-
}
|
|
71
|
-
const location = path.length ? ` at \`${formatIssuePath(path)}\`` : '';
|
|
72
|
-
const value = valueAtPath(root, path);
|
|
73
|
-
const rendered = describeReceived(value);
|
|
74
|
-
// ow named the received type ("expected `number` but received type `string`"). The received value is
|
|
75
|
-
// folded into that clause (``received the string `3` ``) rather than dangling after the location: our
|
|
76
|
-
// custom schemas stop at the expected type, so the clause is appended; zod's built-in messages already
|
|
77
|
-
// end with `, received <type>`, so that tail is replaced with the enriched one.
|
|
78
|
-
let { message } = issue;
|
|
79
|
-
let got = '';
|
|
80
|
-
const bareExpected = BARE_EXPECTED_TYPE_MESSAGE.exec(message);
|
|
81
|
-
const zodReceived = /, received (\S+)$/.exec(message);
|
|
82
|
-
// `arrayOf` messages name the element type — their expected runtime type is `array`.
|
|
83
|
-
const expectedType = bareExpected?.[1].startsWith('an array of') ? 'array' : bareExpected?.[1];
|
|
84
|
-
if (bareExpected && expectedType !== (Number.isNaN(value) ? 'NaN' : describeType(value))) {
|
|
85
|
-
message += `, ${describeReceivedClause(value)}`;
|
|
86
|
-
}
|
|
87
|
-
else if (zodReceived && zodReceived[1] === describeType(value) && rendered !== undefined) {
|
|
88
|
-
message = `${message.slice(0, zodReceived.index)}, ${describeReceivedClause(value)}`;
|
|
89
|
-
}
|
|
90
|
-
else if (rendered !== undefined && !message.endsWith(`received ${rendered}`)) {
|
|
91
|
-
// Messages that never name a received type (regex, min/max, enums) keep the plain value suffix.
|
|
92
|
-
got = `, got \`${rendered}\``;
|
|
93
|
-
}
|
|
94
|
-
return [`${message}${location}${got}`];
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Formats a `ZodError` as a plain, human-readable message that names the
|
|
98
|
-
* offending field *and* the value it received (e.g. ``must match pattern
|
|
99
|
-
* /^[A-Z]{2}$/ at `countryCode`, got `CZE` ``) — closer to the old `ow` errors
|
|
100
|
-
* than zod's default, which omits the received value.
|
|
101
|
-
*/
|
|
102
|
-
function formatZodError(error, root, label) {
|
|
103
|
-
const lines = error.issues.flatMap((issue) => formatIssue(issue, root, []));
|
|
104
|
-
// The label names the validated interface, the way ow's errors ended with "in object `X`".
|
|
105
|
-
return (label ? lines.map((line) => `${line} in \`${label}\``) : lines).join('\n');
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Thrown when an argument fails schema validation.
|
|
109
|
-
*
|
|
110
|
-
* Its `message` is a human-readable sentence naming the offending field and the
|
|
111
|
-
* value it received (rather than a raw JSON dump). The structured
|
|
112
|
-
* {@link https://zod.dev | zod} issues are available on `issues`, and the
|
|
113
|
-
* original `ZodError` on `cause`, for programmatic inspection.
|
|
114
|
-
*/
|
|
115
|
-
export class ArgumentValidationError extends Error {
|
|
116
|
-
/** Structured issues from the underlying schema check. */
|
|
117
|
-
issues;
|
|
118
|
-
/** The raw zod error that triggered this. */
|
|
119
|
-
cause;
|
|
120
|
-
constructor(error, value, label) {
|
|
121
|
-
super(formatZodError(error, value, label), { cause: error });
|
|
122
|
-
this.name = 'ArgumentValidationError';
|
|
123
|
-
this.issues = error.issues;
|
|
124
|
-
this.cause = error;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Parses `value` with `schema`, returning the typed result (with schema defaults applied).
|
|
129
|
-
* Throws {@link ArgumentValidationError} on failure.
|
|
130
|
-
*
|
|
131
|
-
* The optional `label` names the interface being validated and is appended to every error line
|
|
132
|
-
* (e.g. ``… at `maxRequestRetries` in `BasicCrawlerOptions` ``).
|
|
133
|
-
* @internal
|
|
134
|
-
*/
|
|
135
|
-
export function parseArgument(value, schema, label) {
|
|
136
|
-
const result = schema.safeParse(value);
|
|
137
|
-
if (!result.success)
|
|
138
|
-
throw new ArgumentValidationError(result.error, value, label);
|
|
139
|
-
return result.data;
|
|
140
|
-
}
|
|
1
|
+
export { ArgumentValidationError, parseArgument } from '@apify/validations';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@crawlee/utils",
|
|
3
|
-
"version": "4.0.0-beta.
|
|
3
|
+
"version": "4.0.0-beta.166",
|
|
4
4
|
"description": "A set of shared utilities that can be used by crawlers",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.0.0"
|
|
@@ -43,8 +43,9 @@
|
|
|
43
43
|
},
|
|
44
44
|
"dependencies": {
|
|
45
45
|
"@apify/ps-tree": "^1.2.0",
|
|
46
|
-
"@
|
|
47
|
-
"@crawlee/
|
|
46
|
+
"@apify/validations": "^1.1.0",
|
|
47
|
+
"@crawlee/http-client": "4.0.0-beta.166",
|
|
48
|
+
"@crawlee/types": "4.0.0-beta.166",
|
|
48
49
|
"@types/sax": "^1.2.7",
|
|
49
50
|
"cheerio": "^1.0.0",
|
|
50
51
|
"domhandler": "^5.0.3",
|
|
@@ -54,7 +55,7 @@
|
|
|
54
55
|
"tldts": "^7.0.6",
|
|
55
56
|
"tslib": "^2.8.1",
|
|
56
57
|
"whatwg-mimetype": "^4.0.0",
|
|
57
|
-
"zod": "^4.4
|
|
58
|
+
"zod": "^4.5.4"
|
|
58
59
|
},
|
|
59
60
|
"lerna": {
|
|
60
61
|
"command": {
|
|
@@ -63,5 +64,5 @@
|
|
|
63
64
|
}
|
|
64
65
|
}
|
|
65
66
|
},
|
|
66
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "5d34cf3b9da170f22cf5b346014ba44060dad827"
|
|
67
68
|
}
|