Git Product home page Git Product logo

Comments (9)

crdoconnor avatar crdoconnor commented on August 10, 2024

from strictyaml.

crdoconnor avatar crdoconnor commented on August 10, 2024

Hmm, I think I'm inclined to raise an exception in as_document in this case and refuse to accept dicts unless they have at least one item - since without a schema strictyaml won't parse empty dicts.

In theory I think as_document should be able to take them if you give it a schema with EmptyDict though (which would serialize as an empty string).

All comments, suggestions as to the behavior, details from your use case, etc. welcomed, btw. I'm a little unsure how to approach this feature because I've not actually used as_document for anything very major.

from strictyaml.

sio avatar sio commented on August 10, 2024

from strictyaml.

crdoconnor avatar crdoconnor commented on August 10, 2024

I see two solutions here:

  1. My recommended one would be to implement a schema in strictyaml and modify the YAML object directly. You can use the EmptyDict | Map/MapPattern for mappings which could be empty, and when empty it would render to YAML as an empty string.

  2. My less recommended way would be to take your data with empty dicts and transform them into empty strings and vice versa. StrictYAML without a schema can do empty strings, but it can't do empty dicts (or lists).

Unfortunately, while light modification to YAML objects is currently working fine, heavy modification is slightly buggy and so probably won't work for your use case right now (that code is a bit hacky). I will release version 0.14.0 tomorrow which should allow for heavy modification though.

Incidentally, may I ask what you're currently for schema validation instead? Are there things it does that StrictYAML's schema doesn't?

from strictyaml.

sio avatar sio commented on August 10, 2024

Thank you for your reply. I will wait for the next release and see how it goes.

You should probably add that exception you've mentioned or add a note somewhere in the documentation that empty dicts without schema are not supported.

As for validation, I use jsonschema. I chose it because I needed to completely decouple schemas from the code and because I need to be able to validate schemas from third parties. I have considered XSD for a while, but Python tooling for jsonschema seemed much nicer.

I did not consider StrictYAML schemas or other schemas defined with Python code because loading such schemas from third parties without security issues would be next to impossible.

from strictyaml.

crdoconnor avatar crdoconnor commented on August 10, 2024

from strictyaml.

crdoconnor avatar crdoconnor commented on August 10, 2024

Release 0.14.0 took a little longer than I expected, but it's out now. An exception is raised if you attempt to serialize empty dicts or lists:

https://hitchdev.com/strictyaml/using/alpha/howto/build-yaml-document/

However, you can now serialize empty dicts/lists with a schema as documented here:

https://hitchdev.com/strictyaml/using/alpha/scalar/empty/

I'll have a go today at building a prototype that turns JSON schemas in to StrictYAML schema objects. I'll release it as a separate module, however (will need to pip install strictyamljsonschema).

from strictyaml.

crdoconnor avatar crdoconnor commented on August 10, 2024

I've made a start here, but it's quite MVP: https://github.com/hitchdev/strictyamljsonschema

It would help if you let me know which types and what properties on those types you need from here: https://json-schema.org/understanding-json-schema/index.html (e.g. string, integer, number, object (properties, required properties)).

There's a fair bit in there that doesn't look useful to most people.

from strictyaml.

sio avatar sio commented on August 10, 2024

Thank you for devoting so much attention to my issue!

Here are the schemas I'm currently using: one, two, but the idea is to support any JSON schema from any source. At the moment I use the basic value types (string, array, object), limit or allow creating additional properties (additionalProperties), use oneOf and anyOf, specify required fields and field name patterns (patternProperties).

The tricky part in my usecase might be that I need to use separate schemas for the whole document and for several its branches at the same time. E.g. the top-level document allows any dictionary as the branch value, but the branch schema specifies a lot more restrictions on that same dict.

PS: I do not want to mislead you into feeling any extra pressure from me. You should know that you're not blocking my progress in any way. I already have implemented serialization/deserialization routines with JSON/YAML backends and StrictYAML support is mentally tagged as "a nice feature to add when I'm done with everything else". Yours is a great project and even if I don't get to use it this time, I'll keep it in mind for the future :)

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.