Code Monkey home page Code Monkey logo

Comments (4)

gcanti avatar gcanti commented on July 3, 2024 1

I agree that the current behavior is not good.

It's the "expected behavior" given how the messaging system currently works, not in the sense that it's correct overall and especially not in your case: as I wrote above, the current behavior would work fine for refinements of scalar values (such as string or number) but not for refinements of more complex data structures like the case you presented (a refinement of an array of structs).

So the behavior encoded in the formatter needs to be refactored to improve it, which is exactly what I'm doing right now by working on the development branch of the next minor #2634

from effect.

gcanti avatar gcanti commented on July 3, 2024

The behavior is as intended, meaning that when setting a custom message, it takes precedence over everything else (see https://github.com/Effect-TS/effect/blob/main/packages/schema/README.md#custom-error-messages). However, upon reflection, I realize that this logic may work well for scalar value refinements but not for a case like this, where we refine a more complex data structure (such as an array of a struct). Therefore, I'm considering refactoring the message logic in the next minor version of the schema so that the high precedence of the message is opt-in.

In short, the logic could be as follows, given a filter:

  • If there is a custom message defined on it:
    • If this message is marked as override: true, then this message is used unless the error concerns a previous refinement and that refinement has a custom message, in which case the message of that previous refinement is calculated.
    • If the message is NOT marked with override: true:
      • If the error concerns this refinement and not a previous one, its custom message is used.
      • Otherwise, if the error concerns a previous refinement, the corresponding message is calculated.
  • Otherwise, the default message is calculated.

from effect.

canastro avatar canastro commented on July 3, 2024

Just to give more details on my usage as this might be a common use-case.

I have a form which contains a list of items. This list needs to have at least one item. Each item needs to be a string with length > 1 and < 50.
I want to be able to show an error next to the input that is currently invalid.

It would be awkward for the user to start writing their 3rd item in the list, somehow make that item invalid and have an error saying that the list does not have at least 1 item.

I really can't understand how the expected behaviour could be to show a minLength error on an array that has 3 items.

from effect.

canastro avatar canastro commented on July 3, 2024

Awesome, thank you for the quick feedback.

from effect.

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.