Skip to content

Conversation

@ahrjarrett
Copy link
Member

@ahrjarrett ahrjarrett commented Feb 18, 2025

new features

This PR adds the ability for users to set configuration options globally. This is convenient when you have certain cross-cutting requirements, and would like them to be enforced/consistent across the codebase.

example

To set a global configuration option, import configure from the @traversable/schema package.

Note: configuration is optional -- if the defaults suit you, this PR doesn't add any additional hoops to jump through.

Note: successive calls to configure will continue to update the global config object.

import { configure, t } from '@traversable/schema'

const schema = t.object({
  a: t.number,
  b: t.optional(t.string),
})

// before:
schema({
  a: 0,
  b: undefined,
}) // ✅

// with this options, all object and tuple schemas will perform 
// an extra check to make sure that optional properties are either 
// present, or missing altogether, but never present + undefined

configure({ schema: { optionalTreatment: 'exactOptional' } })

// after:
schema({
  a: 0,
  b: undefined,
}) // 🚫 

// Yay, this is the behavior we want 

@ahrjarrett ahrjarrett merged commit ba4ca78 into main Feb 19, 2025
2 checks passed
@github-actions github-actions bot mentioned this pull request Feb 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants