mirror of
https://github.com/Effect-TS/effect.git
synced 2026-12-03 03:25:40 +08:00
@@ -2654,7 +2654,7 @@ export function middlewareDecoding<S extends Top, RD>(
|
||||
) {
|
||||
return (schema: S): middlewareDecoding<S, RD> =>
|
||||
make(
|
||||
AST.decodingMiddleware(schema.ast, new Transformation.Middleware(decode, identity)),
|
||||
AST.middlewareDecoding(schema.ast, new Transformation.Middleware(decode, identity)),
|
||||
{ schema }
|
||||
)
|
||||
}
|
||||
@@ -2696,7 +2696,7 @@ export function middlewareEncoding<S extends Top, RE>(
|
||||
) {
|
||||
return (schema: S): middlewareEncoding<S, RE> =>
|
||||
make(
|
||||
AST.encodingMiddleware(schema.ast, new Transformation.Middleware(identity, encode)),
|
||||
AST.middlewareEncoding(schema.ast, new Transformation.Middleware(identity, encode)),
|
||||
{ schema }
|
||||
)
|
||||
}
|
||||
@@ -5488,13 +5488,16 @@ export interface Redacted<S extends Top> extends
|
||||
export function Redacted<S extends Top>(value: S, options?: {
|
||||
readonly label?: string | undefined
|
||||
}): Redacted<S> {
|
||||
const decodeLabel = typeof options?.label === "string"
|
||||
? Parser.decodeUnknownEffect(Literal(options.label))
|
||||
: undefined
|
||||
const schema = declareConstructor<Redacted_.Redacted<S["Type"]>, Redacted_.Redacted<S["Encoded"]>>()(
|
||||
[value],
|
||||
([value]) => (input, ast, poptions) => {
|
||||
if (Redacted_.isRedacted(input)) {
|
||||
const label: Effect.Effect<void, Issue.Issue, never> = typeof options?.label === "string"
|
||||
const label: Effect.Effect<void, Issue.Issue, never> = decodeLabel !== undefined
|
||||
? Effect.mapErrorEager(
|
||||
Parser.decodeUnknownEffect(Literal(options.label))(input.label, poptions),
|
||||
decodeLabel(input.label, poptions),
|
||||
(issue) => new Issue.Pointer(["label"], issue)
|
||||
)
|
||||
: Effect.void
|
||||
@@ -5529,7 +5532,7 @@ export function Redacted<S extends Top>(value: S, options?: {
|
||||
expected: "Redacted",
|
||||
toCodecJson: ([value]) =>
|
||||
link<Redacted_.Redacted<S["Encoded"]>>()(
|
||||
value,
|
||||
redact(value),
|
||||
{
|
||||
decode: Getter.transform((e) => Redacted_.make(e, { label: options?.label })),
|
||||
encode: Getter.forbidden((oe) =>
|
||||
@@ -5546,6 +5549,40 @@ export function Redacted<S extends Top>(value: S, options?: {
|
||||
return make(schema.ast, { value })
|
||||
}
|
||||
|
||||
/**
|
||||
* @category Redacted
|
||||
* @since 4.0.0
|
||||
*/
|
||||
export interface RedactedFromValue<S extends Top>
|
||||
extends decodeTo<Redacted<toType<S>>, middlewareDecoding<S, S["DecodingServices"]>>
|
||||
{}
|
||||
|
||||
/**
|
||||
* @category Redacted
|
||||
* @since 4.0.0
|
||||
*/
|
||||
export function redact<S extends Top>(schema: S): middlewareDecoding<S, S["DecodingServices"]> {
|
||||
return schema.pipe(middlewareDecoding(Effect.mapErrorEager(Issue.redact)))
|
||||
}
|
||||
|
||||
/**
|
||||
* @category Redacted
|
||||
* @since 4.0.0
|
||||
*/
|
||||
export function RedactedFromValue<S extends Top>(value: S, options?: {
|
||||
readonly label?: string | undefined
|
||||
}): RedactedFromValue<S> {
|
||||
return redact(value).pipe(
|
||||
decodeTo(Redacted(toType(value), options), {
|
||||
decode: Getter.transform((t) => Redacted_.make(t, { label: options?.label })),
|
||||
encode: Getter.forbidden((oe) =>
|
||||
"Cannot encode Redacted" +
|
||||
(Option_.isSome(oe) && typeof oe.value.label === "string" ? ` with label: "${oe.value.label}"` : "")
|
||||
)
|
||||
})
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* @category CauseFailure
|
||||
* @since 4.0.0
|
||||
|
||||
@@ -2117,7 +2117,7 @@ export function applyToLastLink(f: (ast: AST) => AST) {
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function decodingMiddleware(
|
||||
export function middlewareDecoding(
|
||||
ast: AST,
|
||||
middleware: Transformation.Middleware<any, any, any, any, any, any>
|
||||
): AST {
|
||||
@@ -2125,7 +2125,7 @@ export function decodingMiddleware(
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function encodingMiddleware(
|
||||
export function middlewareEncoding(
|
||||
ast: AST,
|
||||
middleware: Transformation.Middleware<any, any, any, any, any, any>
|
||||
): AST {
|
||||
|
||||
@@ -6,6 +6,7 @@ import { format, formatPath, type Formatter as FormatterI } from "./Formatter.ts
|
||||
import * as InternalAnnotations from "./internal/schema/annotations.ts"
|
||||
import * as Option from "./Option.ts"
|
||||
import { hasProperty } from "./Predicate.ts"
|
||||
import * as Redacted from "./Redacted.ts"
|
||||
import type * as Schema from "./Schema.ts"
|
||||
import type * as AST from "./SchemaAST.ts"
|
||||
|
||||
@@ -690,3 +691,28 @@ function formatOption(actual: Option.Option<unknown>): string {
|
||||
if (Option.isNone(actual)) return "no value provided"
|
||||
return format(actual.value)
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function redact(issue: Issue): Issue {
|
||||
switch (issue._tag) {
|
||||
case "MissingKey":
|
||||
return issue
|
||||
case "Forbidden":
|
||||
return new Forbidden(Option.map(issue.actual, Redacted.make), issue.annotations)
|
||||
case "Filter":
|
||||
return new Filter(Redacted.make(issue.actual), issue.filter, redact(issue.issue))
|
||||
case "Pointer":
|
||||
return new Pointer(issue.path, redact(issue.issue))
|
||||
|
||||
case "Encoding":
|
||||
case "InvalidType":
|
||||
case "InvalidValue":
|
||||
case "Composite":
|
||||
return new InvalidValue(Option.map(issue.actual, Redacted.make))
|
||||
|
||||
case "AnyOf":
|
||||
case "OneOf":
|
||||
case "UnexpectedKey":
|
||||
return new InvalidValue(Option.some(Redacted.make(issue.actual)))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,6 +30,18 @@ describe("Config", () => {
|
||||
deepStrictEqual(result, { STRING: "value" })
|
||||
})
|
||||
|
||||
describe("schema", () => {
|
||||
it("should not leak any information about the value", async () => {
|
||||
const provider = ConfigProvider.fromUnknown({})
|
||||
await assertFailure(
|
||||
Config.schema(Schema.Redacted(Schema.Literal("secret")), "a"),
|
||||
provider,
|
||||
`Invalid data <redacted>
|
||||
at ["a"]`
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("constructors", () => {
|
||||
it("fail", async () => {
|
||||
await assertFailure(
|
||||
@@ -139,7 +151,7 @@ describe("Config", () => {
|
||||
await assertFailure(
|
||||
Config.redacted("failure"),
|
||||
provider,
|
||||
`Expected string, got undefined
|
||||
`Invalid data <redacted>
|
||||
at ["failure"]`
|
||||
)
|
||||
})
|
||||
@@ -845,13 +857,13 @@ describe("Config", () => {
|
||||
await assertFailure(
|
||||
config,
|
||||
ConfigProvider.fromEnv({ env: {} }),
|
||||
`Expected string, got undefined
|
||||
`Invalid data <redacted>
|
||||
at ["a"]`
|
||||
)
|
||||
await assertFailure(
|
||||
config,
|
||||
ConfigProvider.fromEnv({ env: { a: "1.1" } }),
|
||||
`Expected an integer, got 1.1
|
||||
`Invalid data <redacted>
|
||||
at ["a"]`
|
||||
)
|
||||
})
|
||||
@@ -1147,7 +1159,7 @@ Expected "Infinity" | "-Infinity" | "NaN", got "a"`
|
||||
await assertFailure(
|
||||
config,
|
||||
ConfigProvider.fromUnknown({ a: "1.1" }),
|
||||
`Expected an integer, got 1.1
|
||||
`Invalid data <redacted>
|
||||
at ["a"]`
|
||||
)
|
||||
})
|
||||
|
||||
@@ -2272,7 +2272,7 @@ Expected a value with a size of at most 2, got Map([["a",1],["b",NaN],["c",3]])`
|
||||
})
|
||||
|
||||
it("Redacted(Finite)", async () => {
|
||||
const schema = Schema.Redacted(Schema.Finite)
|
||||
const schema = Schema.Redacted(Schema.Int)
|
||||
const asserts = new TestSchema.Asserts(schema)
|
||||
|
||||
if (verifyGeneration) {
|
||||
@@ -2288,6 +2288,11 @@ Expected a value with a size of at most 2, got Map([["a",1],["b",NaN],["c",3]])`
|
||||
`Invalid data <redacted>
|
||||
at ["value"]`
|
||||
)
|
||||
await decoding.fail(
|
||||
Redacted.make(1.2),
|
||||
`Invalid data <redacted>
|
||||
at ["value"]`
|
||||
)
|
||||
|
||||
const encoding = asserts.encoding()
|
||||
await encoding.succeed(Redacted.make(123))
|
||||
@@ -2295,12 +2300,65 @@ Expected a value with a size of at most 2, got Map([["a",1],["b",NaN],["c",3]])`
|
||||
await encoding.fail(
|
||||
Redacted.make("a"),
|
||||
`Invalid data <redacted>
|
||||
at ["value"]`
|
||||
)
|
||||
await encoding.fail(
|
||||
Redacted.make(1.2),
|
||||
`Invalid data <redacted>
|
||||
at ["value"]`
|
||||
)
|
||||
})
|
||||
|
||||
it("Redacted(FiniteFromString)", async () => {
|
||||
const schema = Schema.Redacted(Schema.FiniteFromString.check(Schema.isInt()))
|
||||
const asserts = new TestSchema.Asserts(schema)
|
||||
|
||||
if (verifyGeneration) {
|
||||
const arbitrary = asserts.arbitrary()
|
||||
arbitrary.verifyGeneration()
|
||||
}
|
||||
|
||||
const decoding = asserts.decoding()
|
||||
await decoding.succeed(Redacted.make("123"), Redacted.make(123))
|
||||
await decoding.fail(null, `Expected Redacted, got null`)
|
||||
await decoding.fail(
|
||||
Redacted.make(null),
|
||||
`Invalid data <redacted>
|
||||
at ["value"]`
|
||||
)
|
||||
await decoding.fail(
|
||||
Redacted.make("a"),
|
||||
`Invalid data <redacted>
|
||||
at ["value"]`
|
||||
)
|
||||
await decoding.fail(
|
||||
Redacted.make("1.2"),
|
||||
`Invalid data <redacted>
|
||||
at ["value"]`
|
||||
)
|
||||
|
||||
const encoding = asserts.encoding()
|
||||
await encoding.succeed(Redacted.make(123), Redacted.make("123"))
|
||||
await encoding.fail(null, `Expected Redacted, got null`)
|
||||
await encoding.fail(
|
||||
Redacted.make(null),
|
||||
`Invalid data <redacted>
|
||||
at ["value"]`
|
||||
)
|
||||
await encoding.fail(
|
||||
Redacted.make("a"),
|
||||
`Invalid data <redacted>
|
||||
at ["value"]`
|
||||
)
|
||||
await encoding.fail(
|
||||
Redacted.make(1.2),
|
||||
`Invalid data <redacted>
|
||||
at ["value"]`
|
||||
)
|
||||
})
|
||||
|
||||
it("with label", async () => {
|
||||
const schema = Schema.Redacted(Schema.String, { label: "password" })
|
||||
const schema = Schema.Redacted(Schema.NonEmptyString, { label: "password" })
|
||||
const asserts = new TestSchema.Asserts(schema)
|
||||
|
||||
const decoding = asserts.decoding()
|
||||
@@ -2320,6 +2378,39 @@ Expected a value with a size of at most 2, got Map([["a",1],["b",NaN],["c",3]])`
|
||||
`Invalid data <redacted:password>
|
||||
at ["value"]`
|
||||
)
|
||||
|
||||
const encoding = asserts.encoding()
|
||||
await encoding.succeed(Redacted.make("a", { label: "password" }))
|
||||
await encoding.fail(
|
||||
Redacted.make("a", { label: "API key" }),
|
||||
`Expected "password", got "API key"
|
||||
at ["label"]`
|
||||
)
|
||||
await encoding.fail(
|
||||
Redacted.make("", { label: "password" }),
|
||||
`Invalid data <redacted:password>
|
||||
at ["value"]`
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe("RedactedFromValue", () => {
|
||||
it("should not leak any information about the value", async () => {
|
||||
const schema = Schema.RedactedFromValue(Schema.Literal("secret"))
|
||||
const asserts = new TestSchema.Asserts(schema)
|
||||
|
||||
const decoding = asserts.decoding()
|
||||
await decoding.fail(null, `Invalid data <redacted>`)
|
||||
})
|
||||
|
||||
it("should decode a value", async () => {
|
||||
const schema = Schema.RedactedFromValue(Schema.FiniteFromString.check(Schema.isInt()))
|
||||
const asserts = new TestSchema.Asserts(schema)
|
||||
|
||||
const decoding = asserts.decoding()
|
||||
await decoding.succeed("123", Redacted.make(123))
|
||||
await decoding.fail(null, `Invalid data <redacted>`)
|
||||
await decoding.fail("1.2", `Invalid data <redacted>`)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user