Giter VIP home page Giter VIP logo

Comments (11)

ikitommi avatar ikitommi commented on May 13, 2024 5

Proposal: open by default, can be closed via a) schema property :closed and via a value transformer at runtime.

from malli.

ikitommi avatar ikitommi commented on May 13, 2024 3

Closed maps coming via #156

from malli.

ikitommi avatar ikitommi commented on May 13, 2024 1

Thanks for the comments! If schemas were open by default, one could:

  1. use a "close all maps" visitor to close them. Could be a helper for this, e.g. open & close helpers.

  2. one strip away the extras in&out with schema transformers. There is an example of this: https://github.com/metosin/malli/blob/master/README.md#value-transformation

from malli.

lomin avatar lomin commented on May 13, 2024 1

Imho being "spiritual compatible" to clojure.spec regarding open maps is valuable, since malli could piggyback on the thoughts and development which is put into this design philosophy by Cognitect and most of the Clojure community.

from malli.

ikitommi avatar ikitommi commented on May 13, 2024 1

For the question: it's not a nested property: having a top-level closed schema doesn't make the children closed, you have to close those yourself. In schema-tools, there are helpers like open-schema which opens up schemas recursively and with-keyword-keys and with-keys that modify only the given schema. Has worked really well, using actively both variants and there is an issue to port those in to malli, with a polished api most likely.

About merging. plan (#82) is to change mali.core/merge into malli.set/union, which would give clear math on how it should work: becomes open.

union

As with any opinions, might be a good idea to have option to override.

from malli.

ikitommi avatar ikitommi commented on May 13, 2024

This could also be a property of a value transformation like in spec-tools.

from malli.

metametadata avatar metametadata commented on May 13, 2024

I'd vote for closed maps by default because in my code they are the majority of specs and also it seems to be safer by default (e.g. imagine specing the service which sends data to a third party: the closed map will ensure that nothing potentially sensitive slips through the service by mistake).

from malli.

ikitommi avatar ikitommi commented on May 13, 2024

:closed property might not be a good idea. It doesn't compose with :map-of, e.g. getting this to work would be hard:

[:and 
 [:map {:closed true} [:x int?]]
 [:map-of string? string?]]

If merging :map & :map-of( #43) would have an good solution, we could have the closed. But need to figure out how to do that robustly, see #52 for possible errors.

Maybe something like this:

closed map

[:map 
 {:closed true}
 [:x int?]
 [:y int?]]

closed map with extra keys

  • good name for the property? :extra-entry, :extra-entries, :extras, ...???
[:map 
 {:closed true
  :extra-entry [string? string?]}
 [:x int?]
 [:y int?]]

from malli.

ikitommi avatar ikitommi commented on May 13, 2024

Btw, JSON Schema also allows extra keys by default. So, let's hava that as the default.

JSON Schema examples:

  • {"additionalProperties": false} => closed
  • {"additionalProperties": { "type": "string" }} => String values allowed (all keys are Strings anyway)

Also, JSON Schema has the following map constraints:

from malli.

esuomi avatar esuomi commented on May 13, 2024

In practice I've never worked on a long-running system with external integrations(1) where some non-specified fields wouldn't come and go at the weirdest times for various reasons so I'll definitely give my vote for having open schema as default. For team/system internal and those places where strictness has significant value having the option to make the schema closed makes more sense.

Question: Would this be configurable on different levels in nested structures as well? I'm assuming yes, but I also wonder how that would work with composition and whatnot, eg. does merging open+close become open or should it error?

And name for property, how about

  • {:extras/entry [string? string?]} for the quick and simple use case
  • {:extras/key string? ...} when keys are uniform and
  • {:extras/value 'my-transformer} for those really complex cases

(1) that is, integrations to systems done by some other team/company within the same organization or client environment

from malli.

ikitommi avatar ikitommi commented on May 13, 2024

In master. Also, with malli.util/open-schema and malli.util/closed-schema helpers.

from malli.

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.