Skip to content

Latest commit

 

History

History
203 lines (163 loc) · 4.95 KB

README.md

File metadata and controls

203 lines (163 loc) · 4.95 KB

tonic Build Status Dependencies Status

Clojars Project

All of the schemata!

Tonic is a library of generally useful schemata using Prismatic Schema.

Tonic goes well with a healthy dose of gin.

Usage

tonic.char

You can validate lower case letters.

(s/check AlphaLower \q)
;; => nil
(s/check AlphaLower \Q)
;; => (not (:lower-case-alpha-character \Q))

You can validate upper case letters.

(s/check AlphaUpper \Q)
;; => nil
(s/check AlphaUpper \q)
;; => (not (:upper-case-alpha-character \q))

You can validate letters.

(s/check Alpha \q)
;; => nil
(s/check Alpha \Q)
;; => nil
(s/check Alpha :a)
;; => (named (not (some (check % :a) schemas)) :alpha-character)

You can validate numeric digits.

(s/check Digit \4)
;; => nil
(s/check Digit \d)
;; => (named (not (#{\0 \1 \2 \3 \4 \5 \6 \7 \8 \9} \d)) :numeric-digit)

You can validate alpha-numeric characters.

(s/check AlphaNumeric \4)
;; => nil
(s/check AlphaNumeric \q)
;; => nil
(s/check AlphaNumeric \Q)
;; => nil
(s/check AlphaNumeric 7)
;; => (named (not (some (check % 7) schemas)) :alpha-numeric-character)

You can validate lower case hex digits.

(s/check HexLower \5)
;; => nil
(s/check HexLower \f)
;; => nil
(s/check HexLower \F)
;; => (named (not (some (check % \F) schemas)) :hex-lower-case-digit)

You can validate upper case hex digits.

(s/check HexUpper \5)
;; => nil
(s/check HexUpper \F)
;; => nil
(s/check HexUpper \f)
;; => (named (not (some (check % \f) schemas)) :hex-upper-case-digit)

You can validate hex digits irrespective of upper/lower case.

(s/check Hex \5)
;; => nil
(s/check Hex \f)
;; => nil
(s/check Hex \F)
;; => nil
(s/check Hex \g)
;; => (named (not (some (check % \g) schemas)) :hex-digit)

tonic.core

You can validate integral numbers.

(s/check IntegralNumber 12)
;; => nil
(s/check IntegralNumber (long 12))
;; => nil
(s/check IntegralNumber (bigint 12))
;; => nil
(s/check IntegralNumber 12.34)
;; => (named (not (some (check % 12.34) schemas)) :integral-number)

tonic.internet

You can validate IP addresses (IPv4 specifically.)

(s/check IPv4Address "192.168.1.1")
;; => nil
(s/check IPv4Address "192.168.1.256")
;; => (not (:ipv4-address "192.168.1.256"))

You can validate MAC addresses. It's important to realize that there are actually three common formats for displaying MAC addresses, although the most common by far is the colon-separated variant.

(s/check MacAddressColons "a1:b2:c3:d4:e5:f6")
;; => nil
(s/check MacAddressHyphens "a1-b2-c3-d4-e5-f6")
;; => nil
(s/check MacAddressPeriods "a1b2.c3d4.e5f6")
;; => nil
(s/check MacAddress "a1:b2:c3:d4:e5:f6")
;; => nil
(s/check MacAddress "a1-b2-c3-d4-e5-f6")
;; => nil
(s/check MacAddress "a1b2.c3d4.e5f6")
;; => nil
(s/check MacAddress "a1:b2:c3:d4:e5")
;; => (named (not (some (check % "a1:b2:c3:d4:e5") schemas)) :mac-address)
(s/check MacAddress "a1:b2:c3:d4:e5:f6:f6")
;; => (named (not (some (check % a-java.lang.String) schemas)) :mac-address)

tonic.string

You can validate for empty or non-empty strings.

(s/check EmptyString "")
;; => nil
(s/check EmptyString "foo")
;; => (not (:empty-string "foo"))
(s/check NonEmptyString "foo")
;; => nil
(s/check NonEmptyString "")
;; => (not (:non-empty-string ""))
(s/check NonEmptyString :foo)
;; => (not (instance? java.lang.String :foo))

You can validate for a string matching a specific regular expression. You can also provide an optional name for the matched regular expression.

(s/check (re-matches #"[a-z]+") "foo")
;; => nil
(s/check (re-matches #"[a-z]+" :lowercase) "foo123")
;; => (not (:lowercase "foo123"))

You can validate UUID strings. Usually these are all-lowercase, sometimes they are all uppercase (depending on your system.) Mixed case is almost always wrong.

(s/check LowercaseUUID (str (java.util.UUID/randomUUID)))
;; => nil
(s/check LowercaseUUID "c9254dc7-6626-4f94-ab80-9cf43d6666c2")
;; => nil
(s/check UppercaseUUID "C9254DC7-6626-4F94-AB80-9CF43D6666C2")
;; => nil
(s/check StringUUID "c9254dc7-6626-4f94-ab80-9cf43d6666c2")
;; => nil
(s/check StringUUID "C9254DC7-6626-4F94-AB80-9CF43D6666C2")
;; => nil
(s/check StringUUID "I'm not a UUID")
;; => (not (some-matching-either-clause? "I'm not a UUID"))
tonic.string> (s/check StringUUID (java.util.UUID/randomUUID)) ; UUID, but not a string.
;; => (not (some-matching-either-clause? a-java.util.UUID))

License

Copyright © 2015-2017 Christopher Mark Gore, Soli Deo Gloria, all rights reserved.

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.