Giter VIP home page Giter VIP logo

Comments (4)

Meriyemelhajoui avatar Meriyemelhajoui commented on August 27, 2024 1

Thank you for your explanation @sinclairzx81

from typebox.

Meriyemelhajoui avatar Meriyemelhajoui commented on August 27, 2024

@sinclairzx81

from typebox.

sinclairzx81 avatar sinclairzx81 commented on August 27, 2024

@Meriyemelhajoui Hi,

TypeBox currently yields only the top most error for Union-like structures (which includes Enum) but doesn't yield any internal errors of that Union. The reason for this is it tries to prevent excessive error generation for values with non-matching sub variants as these tend to produce excessively large (and sometimes duplicated) error results. Consider the following.

const A = Type.Object({ x: Type.Number(), y: Type.Number() })
const B = Type.Object({ x: Type.String(), y: Type.String() })
const C = Type.Object({ x: Type.Boolean(), y: Type.Boolean() })
const U = Type.Union([A, B, C])

// ...

const E = [...Value.Errors(U, { })] // no properties

// Current (top level only)
//
// [
//  { message: 'Expected union value', path: '' }
// ]
//
// -----
//
// Yielded sub variant errors (not supported)
// [
//  { message: 'Required property', path: '/x' },  // For variant A
//  { message: 'Required property', path: '/y' },
//  { message: 'Required property', path: '/z' },
//
//  { message: 'Required property', path: '/x' },  // For variant B (duplicated)
//  { message: 'Required property', path: '/y' },
//  { message: 'Required property', path: '/z' },
//
//  { message: 'Required property', path: '/x' },  // For variant C (duplicated)
//  { message: 'Required property', path: '/y' },
//  { message: 'Required property', path: '/z' },
//
//   ...
// ]

Unfortunately, I don't know of a better solution to this problem that is both performant and meaningful in terms of errors presented. The generation of the top level Union error isn't ideal, but it is the simplest option I've found thus far. I'm open to community thoughts on a better error generation strategy though.

Better Errors with SetErrorFunction

While it's not possible to yield internal errors for Union, you can provide custom error messages which may be a bit more descriptive of the actual error. You can use the SetErrorFunction and DefaultErrorFunction to achieve this, documentation on this can be found at the link below.

https://github.com/sinclairzx81/typebox#error-function

import { SetErrorFunction, DefaultErrorFunction } from '@sinclair/typebox/errors'
import { Type } from '@sinclair/typebox'
import { Value } from '@sinclair/typebox/value'

// Overrides the default error function to intercept schemas with 
// a errorMessage property. This enable you to assign specific
// errors to types and have them generate in error messages.
SetErrorFunction((param) => ('errorMessage' in param.schema) 
  ? param.schema.errorMessage
  : DefaultErrorFunction(param)
)

const Color = Type.Union([
  Type.Literal('Red'),
  Type.Literal('Blue'),
  Type.Literal('Green'),
], {
  errorMessage: "Expected either 'Red', 'Blue' or 'Green'"
})

// ...

const R = [...Value.Errors(Color, 'Orange')]

console.log(R) // [
               //   {
               //     type: 62,
               //     schema: {
               //       errorMessage: "Expected either 'Red', 'Blue' or 'Green'",
               //       anyOf: [Array],
               //     },
               //     path: '',
               //     value: 'Orange',
               //     message: "Expected either 'Red', 'Blue' or 'Green'"
               //   }
               // ]

Hope this provides a bit of insight into the current Union/Enum error generation (as well as a viable workaround with the SetErrorFunction). I do think better error generation may be possible with the introduction of Tagged/Discriminated Unions (which TypeBox doesn't support natively, but may provide facilities for in later revisions). For now though, the above is the best it can provide under the current setup.

Hope this helps
S

from typebox.

sinclairzx81 avatar sinclairzx81 commented on August 27, 2024

@Meriyemelhajoui Hiya,

Might close off this issue as generating union variant errors is generally not supported (at least for now). As per example though, the SetErrorFunction will allow you to generate type specific error messages if you need a bit more detail than just the generic union error TypeBox currently generates.

If you have any questions on the above or follow up suggestions on how to improve the current TypeBox union / enum generated errors, happy to discuss this further on this thread. Will close for now though.

Cheers!
S

from typebox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.