Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cute-games-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'typestone': minor
---

Changes the default parsing behavior to strip input values from issues, which can be overridden by setting the `exposeInput` flag
2 changes: 1 addition & 1 deletion packages/typestone/src/error/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type Path } from '../internal/process/types.ts';
export interface BaseIssue<TCode extends string> {
readonly code: TCode;
readonly message: string;
readonly input: unknown;
readonly input?: unknown;
readonly path: Path;

readonly abort?: boolean;
Expand Down
9 changes: 8 additions & 1 deletion packages/typestone/src/internal/process/process-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ export function processSchema<
let iteration = iterator.next();

while (!iteration.done) {
const issue = iteration.value;
let issue: Issue;
if (context.exposeInput) {
const { input, ...newIssue } = iteration.value;
issue = newIssue;
} else {
issue = iteration.value;
}

issues.push(issue);
if (issue.abort) break;
iteration = iterator.next();
Expand Down
15 changes: 14 additions & 1 deletion packages/typestone/src/internal/process/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,20 @@ export type Path = PropertyKey[];

export type ProcessMode = 'encode' | 'decode';

export interface ProcessContext<TValue, TMode extends ProcessMode> {
export interface ProcessOptions {
/**
* When `true`, this will expose the `input` value for all issues.
*
* This should be used with caution as exposing the input value can cause
* sensitive information to be leaked.
*/
readonly exposeInput?: boolean;
}

export interface ProcessContext<
TValue,
TMode extends ProcessMode,
> extends ProcessOptions {
readonly value: TValue;
readonly path: Path;
readonly mode: TMode;
Expand Down
6 changes: 4 additions & 2 deletions packages/typestone/src/methods/decode/decode.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { type RawInput, type RawOutput } from '../../def/def.ts';
import { type ProcessOptions } from '../../internal/process/types.ts';
import { type Schema } from '../../schemas/schema/schema.ts';
import { safeDecode } from '../safe-decode/safe-decode.ts';

export type DecodeMethod = <const TSchema extends Schema>(
this: TSchema,
input: RawInput<TSchema>,
options?: ProcessOptions,
) => RawOutput<TSchema>;

export const decode: DecodeMethod = function (input) {
const result = safeDecode.call(this, input);
export const decode: DecodeMethod = function (input, options) {
const result = safeDecode.call(this, input, options);
if (!result.success) throw new Error('Decoding failed.');
return result.data;
};
6 changes: 4 additions & 2 deletions packages/typestone/src/methods/encode/encode.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { type RawInput, type RawOutput } from '../../def/def.ts';
import { type ProcessOptions } from '../../internal/process/types.ts';
import { type Schema } from '../../schemas/schema/schema.ts';
import { safeEncode } from '../safe-encode/safe-encode.ts';

export type EncodeMethod = <const TSchema extends Schema>(
this: TSchema,
output: RawOutput<TSchema>,
options?: ProcessOptions,
) => RawInput<TSchema>;

export const encode: EncodeMethod = function (output) {
const result = safeEncode.call(this, output);
export const encode: EncodeMethod = function (output, options) {
const result = safeEncode.call(this, output, options);
if (!result.success) throw new Error('Encoding failed.');
return result.data;
};
6 changes: 4 additions & 2 deletions packages/typestone/src/methods/parse/parse.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { type RawOutput } from '../../def/def.ts';
import { type ProcessOptions } from '../../internal/process/types.ts';
import { type Schema } from '../../schemas/schema/schema.ts';
import { safeDecode } from '../safe-decode/safe-decode.ts';

export type ParseMethod = <const TSchema extends Schema>(
this: TSchema,
value: unknown,
options?: ProcessOptions,
) => RawOutput<TSchema>;

export const parse: ParseMethod = function (value) {
const result = safeDecode.call(this, value);
export const parse: ParseMethod = function (value, options) {
const result = safeDecode.call(this, value, options);
if (!result.success) throw new Error('Parsing failed.');
return result.data;
};
9 changes: 7 additions & 2 deletions packages/typestone/src/methods/safe-decode/safe-decode.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { type RawInput, type RawOutput } from '../../def/def.ts';
import { processSchema } from '../../internal/process/process-schema.ts';
import { type ParseResult } from '../../internal/process/types.ts';
import {
type ParseResult,
type ProcessOptions,
} from '../../internal/process/types.ts';
import { type Schema } from '../../schemas/schema/schema.ts';

export type SafeDecodeMethod = <const TSchema extends Schema>(
this: TSchema,
input: RawInput<TSchema>,
options?: ProcessOptions,
) => ParseResult<RawOutput<TSchema>>;

export const safeDecode: SafeDecodeMethod = function (input) {
export const safeDecode: SafeDecodeMethod = function (input, options) {
return processSchema(this, {
...options,
mode: 'decode',
value: input,
path: [],
Expand Down
9 changes: 7 additions & 2 deletions packages/typestone/src/methods/safe-encode/safe-encode.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { type RawInput, type RawOutput } from '../../def/def.ts';
import { processSchema } from '../../internal/process/process-schema.ts';
import { type ParseResult } from '../../internal/process/types.ts';
import {
type ParseResult,
type ProcessOptions,
} from '../../internal/process/types.ts';
import { type Schema } from '../../schemas/schema/schema.ts';

export type SafeEncodeMethod = <const TSchema extends Schema>(
this: TSchema,
output: RawOutput<TSchema>,
options?: ProcessOptions,
) => ParseResult<RawInput<TSchema>>;

export const safeEncode: SafeEncodeMethod = function (output) {
export const safeEncode: SafeEncodeMethod = function (output, options) {
return processSchema(this, {
...options,
mode: 'encode',
value: output,
path: [],
Expand Down