Hyperlinkv0.8.0-beta.28

Schema

Source effect/Schema.ts:904248 lines
export interface RedactedFromValue<S extends Constraint>
  extends decodeTo<Redacted<toType<S>>, middlewareDecoding<S, S["DecodingServices"]>>
{
  readonly "Rebuild": RedactedFromValue<S>
}

/**
 * Middleware that wraps decoded errors in `Redacted`, preventing sensitive
 * schema details from leaking in error messages.
 *
 * @category Redacted
 * @since 4.0.0
 */
export function redact<S extends Constraint>(schema: S): middlewareDecoding<S, S["DecodingServices"]> {
  return middlewareDecoding<S, S["DecodingServices"]>(Effect.mapErrorEager(SchemaIssue.redact))(schema)
}

/**
 * Decodes a value and wraps it in `Redacted<A>`. Unlike {@link Redacted} which
 * expects the input to already be a `Redacted` instance, this schema decodes
 * the raw value and wraps it.
 *
 * @see {@link Redacted} for schemas whose input is already a `Redacted` value.
 * @category Redacted
 * @since 4.0.0
 */
export function RedactedFromValue<S extends Constraint>(value: S, options?: {
  readonly label?: string | undefined
  readonly disallowEncode?: boolean | undefined
}): RedactedFromValue<S> {
  return redact(value).pipe(
    decodeTo(
      Redacted(toType(value), {
        label: options?.label,
        disallowJsonEncode: options?.disallowEncode
      }),
      {
        decode: SchemaGetter.transform((t) => Redacted_.make(t, { label: options?.label })),
        encode: options?.disallowEncode ?
          SchemaGetter.forbidden((oe) =>
            "Cannot encode Redacted" +
            (Option_.isSome(oe) && typeof oe.value.label === "string" ? ` with label: "${oe.value.label}"` : "")
          ) :
          SchemaGetter.transform(Redacted_.value)
      }
    )
  )
}