Giter Site home page Giter Site logo

clova's Introduction

clova

A "minimal" validation library for Clojure and ClojureScript.

Status

CircleCI Clojars Project

Installation

clova is available from Clojars

Usage

Validation sets are pairs of keys to validate and the functions used to validate them. When a map conforms to the validation set then the validate function returns the original map.

(validate
  [:email email?
   :age [between? 18 40]
   [:nested :value] [between? 0 10]] 
   {:email "[email protected]" :age 20 :nested {:value 9}})

;; {:email "[email protected]", :age 20, :nested {:value 9}}

When a map does not conform to the validation set then the validate function returns the original map with a sequence of validation errors transposed onto the applicable keys. All validation errors are available using the :clova.core/results key and the validation status is available using the :clova.core/invalid? key.

(validate
  [:email email?
   :age [between? 18 40]
   [:nested :value] [between? 0 10]] 
   {:email "testemail.com" :age 10 :nested {:value 19}})

;; {:clova.core/results ("email should be a valid email address." "age is 10 but it must be between 18 and 40." "nested value is 19 but it must be between 0 and 10.") 
;;  :clova.core/invalid? true 
;;  :email ("email should be a valid email address.") 
;;  :age ("age is 10 but it must be between 18 and 40."), 
;;  :nested {:value ("nested value is 19 but it must be between 0 and 10.")}}

You don't have to use pre-defined validator functions exposed by clova, you can also use arbitrary functions.

Arbitrary functions will not generate scenario specific failure messages but a generic message format of "%s has value %s, which is invalid." will be used.

(validate [:age [> 18]] {:age 21})

;; {:age 21}

If you want to compose multiple validators you can.

(validate [:age required? [greater? 18] [lesser? 30]] {:age 29})

;; {:age 29}

Most of the time it is useful to only apply and fail validation if a given key is present in the map under validation, this is the default behaviour in clova. However if this is not the case and you wish to make a validator fail if the key is not present you can do so by using a required? validator.

(validate [:email required? email?] {:email "[email protected]"})

;; {:email "[email protected]"}

(validate [:age required? [between? 18 30]] {:age 29})

;; {:age 29}

Get the validation status:

(:clova.core/invalid? (validate [:email required? email?] {:email "notanemail"}))

;; true

(:clova.core/invalid? (validate [:email required? email?] {:email "[email protected]"}))

;; nil

or

(valid? [:email required? email?] {:email "[email protected]"})

;; true

(valid? [:email required? email?] {:email "notanemail"})

;; false

Get the validation results (error messages):

(:clova.core/results (validate [:email required? email?] {:email "[email protected]"}))

;; nil
(:clova.core/results (validate [:email required? email?] {:email "notanemail"}))

;; ("email should be a valid email address.")

or

(results [:email required? email?] {:email "[email protected]"})

;; nil

(results [:email required? email?] {:email "notanemail"})

;; ("email should be a valid email address.")

You can also specify a custom function for providing validation error messages. This function will be called with the validator type, the target value and any arguments passed to the validator specified as arguments, if the custom function returns nil then the default validation message will be used.

For example, we can use the between? validator with a custom error message, like so:

(let [message-func (fn [v-type value args]
                    (case v-type
                      :between (str "Age is " value " but it must be between " (first args) " and " (second args))
                       nil))]
    (validate v-set {:age 9} {:default-message-fn message-func}))

By default clova will execute all validators and provide validation messages for all failures. You can override this behaviour using the :short-circuit? option. This will stop execution of subsequent validators after the first validation failure and will therefore only return one validation failure message.

(validate v-set {:email ""} {:short-circuit? true })

See more usage examples.

Validators

clova has the following built in validators

  1. between?
  2. email?
  3. greater?
  4. lesser?
  5. matches?
  6. negative?
  7. positive?
  8. post-code?
  9. not-nil?
  10. required?
  11. url?
  12. zip-code?
  13. length?
  14. longer?
  15. shorter?
  16. one-of?
  17. all?
  18. credit-card?
  19. numeric?
  20. stringy?
  21. date?
  22. before?
  23. after?
  24. =date?
  25. =?
  26. alphanumeric?
  27. not-exists?
  28. exists?

License

Copyright © 2015-2018 Mark Woodhall

Released under the MIT License: http://www.opensource.org/licenses/mit-license.php

clova's People

Contributors

markwoodhall avatar

Stargazers

 avatar  avatar Pedro Delfino avatar Ben Follington avatar  avatar Vitali Perchonok avatar ayato-p avatar Petr Volny avatar Koji Hirano avatar Jason Imison avatar Roman Pearah avatar Mark Godfrey avatar jn avatar Mike Ball avatar Max Savchenko avatar

Watchers

James Cloos avatar  avatar

clova's Issues

Add before? validator

Add a before? validator. Returns true if the validated date is chronologically earlier than the compared date, false otherwise.

Add date? validator

Add a date? validator that uses clj-time and cljs-time and attempts to parse a string value as a date. Returns true if it can parse or false otherwise. It should be possible to specify the date format.

No such var: c/equal?, compiling:(clova/core.cljc:267:7)

hi,

i added clova to my project and require it like this

(:require [clova.core :refer :all])

i think it's ok but i am just a newbie in clojure
anyway, when i run lein run and it starts to compile the app
i get the error in title

Exception in thread "main" java.lang.RuntimeException: No such var: c/equal?, compiling:(clova/core.cljc:267:7)

there's no code using clova yet, so i guess it is a problem in the lib itself.

[clova "0.23.0"] and clojure 1.7.0

Add after? validator

Add a after? validator. Returns true if the validated date is chronologically later than the compared date, false otherwise.

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.