@power-plant/schema 0.0.72 → 0.0.74

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 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, "inputValue", "inputPath", "result", "errors").join("\n")}
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(inputValue) {
332
- const errors = [];
306
+ export function parseSafe(value: unknown) {
307
+ const errors: { path: string; failure: string }[] = [];
333
308
 
334
309
  let result;
335
- ${generateStatements(schema, "inputValue", "\"$\"", "result", "errors").join("\n")}
310
+ ${generateStatements(schema, "value", "\"$\"", "result", "errors").join("\n")}
336
311
 
337
312
  if (errors.length > 0) {
338
313
  return errors;
@@ -351,8 +326,8 @@ export function parseSafe(inputValue) {
351
326
  * @returns The parsed value conforming to the schema.
352
327
  * @throws {Error} When the input value cannot be parsed into a valid result according to the schema.
353
328
  */
354
- export function parse(inputValue) {
355
- const result = parseSafe(inputValue);
329
+ export function parse(value: unknown) {
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, "inputValue", "inputPath", "result", "errors").join("\n")}
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(inputValue) {
331
- const errors = [];
305
+ export function parseSafe(value: unknown) {
306
+ const errors: { path: string; failure: string }[] = [];
332
307
 
333
308
  let result;
334
- ${generateStatements(schema, "inputValue", "\"$\"", "result", "errors").join("\n")}
309
+ ${generateStatements(schema, "value", "\"$\"", "result", "errors").join("\n")}
335
310
 
336
311
  if (errors.length > 0) {
337
312
  return errors;
@@ -350,8 +325,8 @@ export function parseSafe(inputValue) {
350
325
  * @returns The parsed value conforming to the schema.
351
326
  * @throws {Error} When the input value cannot be parsed into a valid result according to the schema.
352
327
  */
353
- export function parse(inputValue) {
354
- const result = parseSafe(inputValue);
328
+ export function parse(value: unknown) {
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.72",
3
+ "version": "0.0.74",
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.588",
172
+ "@powerlines/plugin-tsdown": "^0.1.589",
173
173
  "@types/node": "^25.9.5",
174
- "powerlines": "^0.47.187",
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": "dbd70c5c716502f821cd95c24a92e8af7752b9ae"
188
+ "gitHead": "bb0ad8b94b0eb600e56b87605d7196fb7275727b"
189
189
  }