Git Product home page Git Product logo

Comments (4)

andres-fr avatar andres-fr commented on August 10, 2024 1

Hi, sorry for reopening. I'm new to strictyaml, it was an eye-opener to me for some daunting issues with regular YAML. Thanks!

I just wanted to point out that it is taking me a while to figure out how to validate YAML objects when they are already in-memory, without going through the process of re-loading them.

As far as I can tell, the documentation shows validation only in the context of load(data, schema). After searching through the code, I found the YAML.revalidate method, which seems to fit the bill:

* Revalidated with a stricter schema (.revalidate(schema)).

Is this correct? If not, what would be the best way to subject an existing (potentially mutable) YAML object to a specific validation schema?

Cheers and thanks again for the amazing SW!
aferro

from strictyaml.

crdoconnor avatar crdoconnor commented on August 10, 2024

Yep, exactly right. Those should have been in there. I've just added them now. Thanks for pointing it out.

from strictyaml.

AlexandreDecan avatar AlexandreDecan commented on August 10, 2024

You should also mention that "OR" is satisfied from "left to right", meaning that if you have a field that contains 42, then a validator Str() | Int() will map the value to a string, and a validator Int() | Str() will map the value to an integer. Is it right? :-)

from strictyaml.

andres-fr avatar andres-fr commented on August 10, 2024

After trying things out a little more it seems now to me that .revalidate does not fit the bill.

One important feature of strictyaml is that contents are loaded as strings unless specified otherwise via a schema. This must happen upon load in the lifecycle, and not after.

For this reason, if a YAML object has been loaded as a string, calling revalidate with a specific schema will trigger an error containing something like the following:

my_yaml.revalidate(my_schema)
*** strictyaml.exceptions.YAMLValidationError: when expecting an integer
  in "<unicode string>", line 205, column 1:
      x: '123'
    ^ (line: 205)
found arbitrary text

The give away is the found arbitrary text error.

So I guess the only meaningful solution is indeed to convert to string via as_yaml and then parse again with the specific schema, as follows:

validated_yaml = strictyaml.load(initial_yaml.as_yaml(), schema=MY_SCHEMA)

Did I get it right this time?

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.