Effect-TS / effect

An ecosystem of tools to build robust applications in TypeScript
https://effect.website
MIT License
7.71k stars 245 forks source link

New methods `extractAll` and `extractSchema` to `UrlParams` (added `Schema.BooleanFromString`) #3900

Closed SandroMaglione closed 1 week ago

SandroMaglione commented 2 weeks ago

Type

Description

Added extractAll and extractSchema to UrlParams. They allow to extract all key-value pairs from UrlParams and validate them using a given Schema.

const urlParams = UrlParams.fromInput({ a: 1, b: true, c: "string", e: [1, 2, 3] })
const result = UrlParams.extractAll(urlParams)
assert.deepStrictEqual(
  result,
  { "a": "1", "b": "true", "c": "string", "e": ["1", "2", "3"] }
)
Effect.gen(function*() {
  const urlParams = UrlParams.fromInput({ "a": [10, "string"], "b": false })
  const result = yield* UrlParams.extractSchema(Schema.Struct({
    a: Schema.Tuple(Schema.NumberFromString, Schema.String),
    b: Schema.BooleanFromString
  }))(urlParams)

  assert.deepStrictEqual(result, {
    a: [10, "string"],
    b: false
  })
}))

The PR also added a new Schema.BooleanFromString to Schema.

Motivation

When using searchParams from frameworks like next, I often need to validate the correct shape using a Schema. The params have this type:

type SearchParamsNext = Promise<{ [key: string]: string | string[] | undefined }>;

Using fromInput + extractSchema allows to make sure that the params are valid (or handle a ParseError otherwise).

changeset-bot[bot] commented 2 weeks ago

🦋 Changeset detected

Latest commit: 7c758e48c12aa042b10676f191b86d52d958fb8d

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 34 packages | Name | Type | | ------------------------------- | ----- | | @effect/platform | Major | | effect | Minor | | @effect/cli | Major | | @effect/cluster-node | Major | | @effect/cluster-workflow | Major | | @effect/cluster | Major | | @effect/experimental | Major | | @effect/platform-browser | Major | | @effect/platform-bun | Major | | @effect/platform-node-shared | Major | | @effect/platform-node | Major | | @effect/rpc-http | Major | | @effect/rpc | Major | | @effect/sql-clickhouse | Major | | @effect/sql-d1 | Major | | @effect/sql-libsql | Major | | @effect/sql-mssql | Major | | @effect/sql-mysql2 | Major | | @effect/sql-pg | Major | | @effect/sql-sqlite-bun | Major | | @effect/sql-sqlite-node | Major | | @effect/sql | Major | | @effect/ai | Major | | @effect/ai-openai | Major | | @effect/cluster-browser | Major | | @effect/opentelemetry | Major | | @effect/printer-ansi | Major | | @effect/printer | Major | | @effect/sql-drizzle | Major | | @effect/sql-kysely | Major | | @effect/sql-sqlite-react-native | Major | | @effect/sql-sqlite-wasm | Major | | @effect/typeclass | Major | | @effect/vitest | Major |

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR