Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to preserve excess properties from S.decode, S.parse #2008

Closed
alex-dixon opened this issue Jan 30, 2024 · 0 comments · Fixed by #2011 or #2012
Closed

Add option to preserve excess properties from S.decode, S.parse #2008

alex-dixon opened this issue Jan 30, 2024 · 0 comments · Fixed by #2011 or #2012
Assignees
Labels
enhancement New feature or request schema

Comments

@alex-dixon
Copy link
Contributor

What is the problem this feature would solve?

The default behavior of S.parse and S.decode is to ignore excess properties. This means S.decode will select only the keys of the data that a schema specifies.

Example:

const data = S.parseSync({foo: S.string})({foo: 'ok', bar: 'ok'})
data.bar  => undefined

Sometimes, this is not desirable. This feature would allow users to opt-in to preserving excess properties at runtime for all schemas without needing to write their schemas any differently.

Users of fp-ts/io-ts migrating to Effect may benefit from having this option available as it would mirror the default behavior of io-ts's decode function, which is roughly equivalent to S.parse and S.decode in Schema.

What is the feature you are proposing to solve the problem?

ParseOptions is currently defined as:

/**
 * @category model
 * @since 1.0.0
 */
export interface ParseOptions {
  /** default "first" */
  readonly errors?: "first" | "all"
  /** default "ignore" */
  readonly onExcessProperty?: "ignore" | "error"
}

onExcessProperty could add a "preserve" option. When provided this option would alter the behavior of S.parse and S.decode such that they do not error on unknown keys or strip them at runtime.

What alternatives have you considered?

I have been trying solutions in userland since April of last year. Wrapping existing types that should have this behavior with S.extend(type, S.record(S.string, S.any)) is difficult to enforce across teams and large codebases. It has perhaps seemed obtuse when the addition is solely to force certain runtime behavior of S.parse (for example, to preserve data sent to HTTP endpoints in logs). The cost of this can be significant (data loss, confusion between clients and devs about what is being sent or not).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request schema
Projects
Archived in project
2 participants