Code Monkey home page Code Monkey logo

Comments (14)

crdoconnor avatar crdoconnor commented on May 18, 2024 1

Thanks. And thanks once again for pointing it out.

I'm working on preventing the above bug from occurring by preventing the combination of Maps / Seqs using the Or validator. I'll put a message hinting that the end user should use revalidation for their purposes so future users won't fall down the same rabbit hole.

from strictyaml.

crdoconnor avatar crdoconnor commented on May 18, 2024

Yeah, this is an unfortunate bug. Thank you for bringing it to my attention.

It's a natural side effect of the stateful way in which mappings are parsed via the validator (I did it statelessly for a while but that made validations take an unconscionably long time to parse).

This can, of course, be avoided by using Optional, which is the recommended way of parsing the way you just did:

schema = Map(
    {
        "configuration": MapPattern(
            Str(), 
            Map({
                "helptext": Str(), 
                Optional("min"): Int(), 
            }),
            minimum_keys=1
        ),
    }
)

I'm considering a 'fix' being to prevent Or validation combinations which include more than one Map. I had originally intended Or validation to solely be used like so:

"EmptyNone() | Str() | Map({...})"

Rather than as a way of combining Maps:

"Map({...}) | Map({....}).

from strictyaml.

kinkerl avatar kinkerl commented on May 18, 2024

Hey, thank you for the response.

My original usecase and how i found the bug is a little more complex than the example i used above. I don't think i can achieve this with Optional:

I want to define and validate a list of components in YAML. A component can be of a certain type (A or B) and each component type can have its own attributes, depending on the type. For example we have for component A:

 Map({
    "type": Str(),  # type A
    "helptext": Str(), 
}),

And component B looks like this:

 Map({
    "type": Str(),  # type B
    "min": Int(), 
    "max": Int(), 
}),

And i would like to create list that consists, for example, of three times component A and two times component B and then again tree times component A. The order is not fix, but it is important. (just as a random example, the numbers can change)

Shorter example:

 configuration
  component_a:
    type: A
    helptext: "some text"
  component_b:
    type: B
    min: 1
    max: 10
  component_a:
    type: A
    helptext: "some other text"

It relativly easy to write the YAML for that but i have a super hard time to find a way to create a schema for that, given that the validator is stateful.

Do you think it is possible to validate the YAML with the limitations?

from strictyaml.

crdoconnor avatar crdoconnor commented on May 18, 2024

from strictyaml.

crdoconnor avatar crdoconnor commented on May 18, 2024

from strictyaml.

crdoconnor avatar crdoconnor commented on May 18, 2024

Looks like I won't be able to get it done this weekend, but I'm still working on it.

from strictyaml.

kinkerl avatar kinkerl commented on May 18, 2024

No worries. Anything i can help you with?

from strictyaml.

crdoconnor avatar crdoconnor commented on May 18, 2024

from strictyaml.

kinkerl avatar kinkerl commented on May 18, 2024

I think i understand what you are getting at but i have zero idea how to do that, yet :)

Essentially, this would move away from a "static" large schema definition to a more dynamic validation, correct?

from strictyaml.

crdoconnor avatar crdoconnor commented on May 18, 2024

from strictyaml.

crdoconnor avatar crdoconnor commented on May 18, 2024

from strictyaml.

kinkerl avatar kinkerl commented on May 18, 2024

ha, this actually works well!

I think we should leave the ticket open for the original bug but my problem is solved for now. I can work with that. Thank you very much!

from strictyaml.

crdoconnor avatar crdoconnor commented on May 18, 2024

OrValidation is now disallowed using multiple MapValidators in version 1.0.1.

from strictyaml.

kumekay avatar kumekay commented on May 18, 2024

I have a small question,
Though it's not stated that project version is following semantic versioning,
my general feeling says that patch version update shouldn't introduce breaking changes.
Do you have some policy about versioning for this project?

from strictyaml.

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.