@power-plant/schema 0.0.73 → 0.0.75
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/dist/codegen.cjs +9 -34
- package/dist/codegen.mjs +9 -34
- package/package.json +4 -4
package/dist/codegen.cjs
CHANGED
|
@@ -287,34 +287,9 @@ function generateParserCode(schema, options = {}) {
|
|
|
287
287
|
lines.push(` ${resultVar}.push(itemResult);`, ` }`, ` ${targetVar} = ${resultVar};`, `}`);
|
|
288
288
|
return lines;
|
|
289
289
|
}
|
|
290
|
-
return
|
|
291
|
-
* Parser error constructor function.
|
|
292
|
-
*/
|
|
293
|
-
function ParserError(path, failure) {
|
|
294
|
-
this.path = path;
|
|
295
|
-
this.failure = failure;
|
|
296
|
-
this.name = "ParserError";
|
|
297
|
-
this.message = path + ": " + failure;
|
|
298
|
-
}
|
|
299
|
-
ParserError.prototype = Object.create(Error.prototype);
|
|
300
|
-
ParserError.prototype.constructor = ParserError;
|
|
301
|
-
|
|
302
|
-
/**
|
|
303
|
-
* Represents a parser error with path and failure message.
|
|
304
|
-
*/
|
|
305
|
-
export class ParserError extends Error {
|
|
306
|
-
constructor(
|
|
307
|
-
public path: string,
|
|
308
|
-
public failure: string
|
|
309
|
-
) {
|
|
310
|
-
super(\`\${path}: \${failure}\`);
|
|
311
|
-
this.name = "ParserError";
|
|
312
|
-
}
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
${Object.entries(definitions).map(([name, definition]) => `function ${toParserIdentifier(name)}(inputValue, inputPath, errors) {
|
|
290
|
+
return `${Object.entries(definitions).map(([name, definition]) => `function ${toParserIdentifier(name)}(value: unknown, path: string, errors: { path: string; failure: string }[]) {
|
|
316
291
|
let result;
|
|
317
|
-
${generateStatements(definition, "
|
|
292
|
+
${generateStatements(definition, "value", "path", "result", "errors").join("\n")}
|
|
318
293
|
|
|
319
294
|
return result;
|
|
320
295
|
}`).join("\n\n")}
|
|
@@ -328,11 +303,11 @@ ${generateStatements(definition, "inputValue", "inputPath", "result", "errors").
|
|
|
328
303
|
* @param value - The input value to parse.
|
|
329
304
|
* @returns The parsed value conforming to the schema or an array of validation errors.
|
|
330
305
|
*/
|
|
331
|
-
export function parseSafe(
|
|
332
|
-
const errors = [];
|
|
306
|
+
export function parseSafe(value: unknown): ${stringifyType(schema)} | { path: string; failure: string }[] {
|
|
307
|
+
const errors: { path: string; failure: string }[] = [];
|
|
333
308
|
|
|
334
|
-
let result;
|
|
335
|
-
${generateStatements(schema, "
|
|
309
|
+
let result!: ${stringifyType(schema)};
|
|
310
|
+
${generateStatements(schema, "value", "\"$\"", "result", "errors").join("\n")}
|
|
336
311
|
|
|
337
312
|
if (errors.length > 0) {
|
|
338
313
|
return errors;
|
|
@@ -349,10 +324,10 @@ export function parseSafe(inputValue) {
|
|
|
349
324
|
*
|
|
350
325
|
* @param value - The input value to parse.
|
|
351
326
|
* @returns The parsed value conforming to the schema.
|
|
352
|
-
* @throws
|
|
327
|
+
* @throws When the input value cannot be parsed into a valid result according to the schema.
|
|
353
328
|
*/
|
|
354
|
-
export function parse(
|
|
355
|
-
const result = parseSafe(
|
|
329
|
+
export function parse(value: unknown): ${stringifyType(schema)} {
|
|
330
|
+
const result = parseSafe(value);
|
|
356
331
|
if (Array.isArray(result) && result.length > 0 && result.every(error => "path" in error && typeof error.path === "string" && error.path && "failure" in error && typeof error.failure === "string" && error.failure)) {
|
|
357
332
|
throw new Error(\`The following validation errors occurred while parsing the input value: \\n\${Object.entries(result.reduce((acc, error) => ((acc[error.path] ??= []).push(error.failure), acc), {})).map(([path, failures]) => \`\${path.replace(/^\\$\\./, "") ? \`\${path.replace(/^\\$\\./, "")}: \\n\` : ""}\${failures.map(failure => \` - \${failure}\`).join("\\n")}\`).join("\\n")}\`);
|
|
358
333
|
}
|
package/dist/codegen.mjs
CHANGED
|
@@ -286,34 +286,9 @@ function generateParserCode(schema, options = {}) {
|
|
|
286
286
|
lines.push(` ${resultVar}.push(itemResult);`, ` }`, ` ${targetVar} = ${resultVar};`, `}`);
|
|
287
287
|
return lines;
|
|
288
288
|
}
|
|
289
|
-
return
|
|
290
|
-
* Parser error constructor function.
|
|
291
|
-
*/
|
|
292
|
-
function ParserError(path, failure) {
|
|
293
|
-
this.path = path;
|
|
294
|
-
this.failure = failure;
|
|
295
|
-
this.name = "ParserError";
|
|
296
|
-
this.message = path + ": " + failure;
|
|
297
|
-
}
|
|
298
|
-
ParserError.prototype = Object.create(Error.prototype);
|
|
299
|
-
ParserError.prototype.constructor = ParserError;
|
|
300
|
-
|
|
301
|
-
/**
|
|
302
|
-
* Represents a parser error with path and failure message.
|
|
303
|
-
*/
|
|
304
|
-
export class ParserError extends Error {
|
|
305
|
-
constructor(
|
|
306
|
-
public path: string,
|
|
307
|
-
public failure: string
|
|
308
|
-
) {
|
|
309
|
-
super(\`\${path}: \${failure}\`);
|
|
310
|
-
this.name = "ParserError";
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
|
|
314
|
-
${Object.entries(definitions).map(([name, definition]) => `function ${toParserIdentifier(name)}(inputValue, inputPath, errors) {
|
|
289
|
+
return `${Object.entries(definitions).map(([name, definition]) => `function ${toParserIdentifier(name)}(value: unknown, path: string, errors: { path: string; failure: string }[]) {
|
|
315
290
|
let result;
|
|
316
|
-
${generateStatements(definition, "
|
|
291
|
+
${generateStatements(definition, "value", "path", "result", "errors").join("\n")}
|
|
317
292
|
|
|
318
293
|
return result;
|
|
319
294
|
}`).join("\n\n")}
|
|
@@ -327,11 +302,11 @@ ${generateStatements(definition, "inputValue", "inputPath", "result", "errors").
|
|
|
327
302
|
* @param value - The input value to parse.
|
|
328
303
|
* @returns The parsed value conforming to the schema or an array of validation errors.
|
|
329
304
|
*/
|
|
330
|
-
export function parseSafe(
|
|
331
|
-
const errors = [];
|
|
305
|
+
export function parseSafe(value: unknown): ${stringifyType(schema)} | { path: string; failure: string }[] {
|
|
306
|
+
const errors: { path: string; failure: string }[] = [];
|
|
332
307
|
|
|
333
|
-
let result;
|
|
334
|
-
${generateStatements(schema, "
|
|
308
|
+
let result!: ${stringifyType(schema)};
|
|
309
|
+
${generateStatements(schema, "value", "\"$\"", "result", "errors").join("\n")}
|
|
335
310
|
|
|
336
311
|
if (errors.length > 0) {
|
|
337
312
|
return errors;
|
|
@@ -348,10 +323,10 @@ export function parseSafe(inputValue) {
|
|
|
348
323
|
*
|
|
349
324
|
* @param value - The input value to parse.
|
|
350
325
|
* @returns The parsed value conforming to the schema.
|
|
351
|
-
* @throws
|
|
326
|
+
* @throws When the input value cannot be parsed into a valid result according to the schema.
|
|
352
327
|
*/
|
|
353
|
-
export function parse(
|
|
354
|
-
const result = parseSafe(
|
|
328
|
+
export function parse(value: unknown): ${stringifyType(schema)} {
|
|
329
|
+
const result = parseSafe(value);
|
|
355
330
|
if (Array.isArray(result) && result.length > 0 && result.every(error => "path" in error && typeof error.path === "string" && error.path && "failure" in error && typeof error.failure === "string" && error.failure)) {
|
|
356
331
|
throw new Error(\`The following validation errors occurred while parsing the input value: \\n\${Object.entries(result.reduce((acc, error) => ((acc[error.path] ??= []).push(error.failure), acc), {})).map(([path, failures]) => \`\${path.replace(/^\\$\\./, "") ? \`\${path.replace(/^\\$\\./, "")}: \\n\` : ""}\${failures.map(failure => \` - \${failure}\`).join("\\n")}\`).join("\\n")}\`);
|
|
357
332
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@power-plant/schema",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.75",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "A Power Plant utility package to help managing schemas.",
|
|
6
6
|
"keywords": [
|
|
@@ -169,9 +169,9 @@
|
|
|
169
169
|
},
|
|
170
170
|
"devDependencies": {
|
|
171
171
|
"@deepkit/type-compiler": "1.0.19",
|
|
172
|
-
"@powerlines/plugin-tsdown": "^0.1.
|
|
172
|
+
"@powerlines/plugin-tsdown": "^0.1.589",
|
|
173
173
|
"@types/node": "^25.9.5",
|
|
174
|
-
"powerlines": "^0.47.
|
|
174
|
+
"powerlines": "^0.47.188",
|
|
175
175
|
"valibot": "^1.4.2",
|
|
176
176
|
"zod": "^4.4.3"
|
|
177
177
|
},
|
|
@@ -185,5 +185,5 @@
|
|
|
185
185
|
},
|
|
186
186
|
"publishConfig": { "access": "public" },
|
|
187
187
|
"inlinedDependencies": { "@deepkit/type-compiler": "1.0.19" },
|
|
188
|
-
"gitHead": "
|
|
188
|
+
"gitHead": "5030d7876addbb22c300e7e5ee8ae5b505e6aa9f"
|
|
189
189
|
}
|