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

Revision 0.33.16 #1028

Merged
merged 2 commits into from
Oct 11, 2024
Merged

Revision 0.33.16 #1028

merged 2 commits into from
Oct 11, 2024

Conversation

sinclairzx81
Copy link
Owner

@sinclairzx81 sinclairzx81 commented Oct 11, 2024

This PR is a publishing prepare for #1015. The update here enables Union errors to be obtained as a sub property of ValueError. The interior errors are of type ValueErrorIterator meaning to obtain each error for each variant, the caller will need to pull back errors through the iterator.

The following is example demonstrates pulling back union errors using two recursive functions.

import { ValueError, ValueErrorIterator } from '@sinclair/typebox/compiler'
import { Value } from '@sinclair/typebox/value'
import { Type } from '@sinclair/typebox'

// ------------------------------------------------------------------
// Type
// ------------------------------------------------------------------

const T = Type.Union([
  Type.Object({ x: Type.Number(), y: Type.Number(), z: Type.Number() }),
  Type.Object({ a: Type.Number(), b: Type.Number(), c: Type.Number() })
])

// ------------------------------------------------------------------
// Recursive Map Interior Errors
// ------------------------------------------------------------------
interface MappedValueError {
  message: string
  path: string
  value: unknown
  errors: MappedValueError[][]
}
function MapValueError(error: ValueError): MappedValueError {
  const { message, path, value } = error
  const errors = error.errors.map(error => MapValueErrorIterator(error)) // .flat()
  return { message, path, value, errors }
}
function MapValueErrorIterator(iterator: ValueErrorIterator): MappedValueError[] {
  return [...iterator].map(error => MapValueError(error))
}

// ------------------------------------------------------------------
// Usage
// ------------------------------------------------------------------

const E = Value.Errors(T, { x: true, y: true, z: true })

const M = MapValueErrorIterator(E)

console.dir(M, { depth: 100 })

Additional updates include bumping the TS compiler to latest (5.6.3)

@sinclairzx81 sinclairzx81 merged commit 295f8d9 into master Oct 11, 2024
18 checks passed
@sinclairzx81 sinclairzx81 deleted the publish branch October 11, 2024 01:41
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.

1 participant