Last active
July 8, 2019 11:52
-
-
Save tommmyy/36c6707c7997c2b0c1021d5107a6270b to your computer and use it in GitHub Desktop.
How to create reusable field validations with @validarium?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Did you know that in @validarium you can combine not only validation schemes | |
// (as is used in `validate` fn), but even the particular validations? | |
import { validate, createValidation, combineValidate } from '@validarium/core'; | |
// 1. For the example I am using one of the validation from @validarium/validations: | |
import { hasNoSpecialSymbols } from '@validarium/validations'; | |
import { test } from 'ramda'; | |
import m from './messages'; | |
// 2. Then I am creating my own validation: | |
const hasNoWhiteSpace = createValidation(test(/^[\S]*$/), m.noWhiteSpaces); | |
// `hasNoWhiteSpace` is pretty reusable so it should be placed somewhere in my utils package. | |
// Than I have need to create validation fn for `username` field. | |
// The field is probably part of several entities such as `user` or `company`... etc. | |
// So why not declare all the validations for this field in one place? | |
export const isValidUsername = combineValidate(hasNoSpecialSymbols, hasNoWhiteSpace); | |
// ... | |
const fieldValidations = validate({ username: [isRequired, isValidUsername]); | |
reduxForm({ validate: fieldValidations })(MyForm) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment