schema-conformer

A library for configurable conforming of data according to prismatic schemas.

License

License

Categories

Categories

ORM Data
GroupId

GroupId

com.vodori
ArtifactId

ArtifactId

schema-conformer
Last Version

Last Version

0.1.2
Release Date

Release Date

Type

Type

jar
Description

Description

schema-conformer
A library for configurable conforming of data according to prismatic schemas.
Project URL

Project URL

https://github.com/vodori/schema-conformer
Source Code Management

Source Code Management

https://github.com/vodori/schema-conformer

Download schema-conformer

How to add to project

<!-- https://jarcasting.com/artifacts/com.vodori/schema-conformer/ -->
<dependency>
    <groupId>com.vodori</groupId>
    <artifactId>schema-conformer</artifactId>
    <version>0.1.2</version>
</dependency>
// https://jarcasting.com/artifacts/com.vodori/schema-conformer/
implementation 'com.vodori:schema-conformer:0.1.2'
// https://jarcasting.com/artifacts/com.vodori/schema-conformer/
implementation ("com.vodori:schema-conformer:0.1.2")
'com.vodori:schema-conformer:jar:0.1.2'
<dependency org="com.vodori" name="schema-conformer" rev="0.1.2">
  <artifact name="schema-conformer" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.vodori', module='schema-conformer', version='0.1.2')
)
libraryDependencies += "com.vodori" % "schema-conformer" % "0.1.2"
[com.vodori/schema-conformer "0.1.2"]

Dependencies

compile (2)

Group / Artifact Type Version
org.clojure : clojure jar 1.10.1
prismatic » schema jar 1.1.12

test (1)

Group / Artifact Type Version
clj-time » clj-time jar 0.15.2

Project Modules

There are no modules declared in this project.

Build Status Maven metadata URL

Problem

We're users of prismatic/schema. Schema is expressive and useful but we always find ourselves using custom matchers in order to be liberal in what we accept and conservative in what we send. The basic matchers bundled in Schema aren't enough.

Installation

[com.vodori/schema-conformer "0.1.2"]

Usage

(require '[schema-conformer.core :as scc]
         '[clojure.pprint :as pprint]
         '[schema.core :as s])

(s/defschema TestSchema
  {:set-of-strings                  #{s/Str}
   :required-field                  [s/Keyword]
   (s/optional-key :optional-field) (s/maybe s/Str)
   :default-value-field             (scc/default s/Str "NOT_FOUND")
   :enum-of-keywords                (s/enum :one :two)})

(def data
  {:set-of-strings   [:example]
   :enum-of-keywords "one"
   :optional-field   nil})

(pprint/pprint (scc/conform TestSchema data))

{:set-of-strings      #{"example"},  ; converted vector to a set, converted keywords to strings
 :required-field      [],            ; added missing required keys, set a missing required collection to empty
 :default-value-field "NOT_FOUND",   ; added missing required key with the default value set by the schema
 :enum-of-keywords    :one}          ; converted the string to a keyword because the enum used a keyword value

Options

By default, all coercions are enabled. You can disable specific coercions by passing an options map. Available options are:

{:align-map-keys      true,
 :constrained->nested true,
 :datetime->string    true,
 :default->nested     true,
 :either->nested      true,
 :enum->nested        true,
 :instant->string     true,
 :integer->datetime   true,
 :integer->instant    true,
 :keyword->string     true,
 :keyword->symbol     true,
 :list->set           true,
 :list->vector        true,
 :nil->abstract       true,
 :nil->extension      true,
 :nil->map            true,
 :nil->set            true,
 :nil->vector         true,
 :number->boolean     true,
 :object-id->string   true,
 :set->vector         true,
 :string->boolean     true,
 :string->datetime    true,
 :string->instant     true,
 :string->keyword     true,
 :string->object-id   true,
 :string->symbol      true,
 :string->uuid        true,
 :symbol->keyword     true,
 :symbol->string      true,
 :uuid->string        true,
 :vector->set         true}

Classpath Specific Coercions

If joda time is on your classpath:

  • Converting org.joda.time.DateTime to ISO strings
  • Converting ISO strings to org.joda.time.DateTime

If mongodb is on your classpath:

  • Converting org.bson.types.ObjectId to strings
  • Converting strings to org.bson.types.ObjectId

Production Notes

Be sure to create a conformer once for your schema and reuse it for each piece of data rather than calling conform each time (same as you would with Schema).


Alternatives

  • Malli is a nicer data-driven alternative to Schema.

License

This project is licensed under MIT license.

com.vodori

Vodori

We create content management products suitable for regulated domains.

Versions

Version
0.1.2
0.1.1
0.1.0