-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
updated docs #16
updated docs #16
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,10 +2,12 @@ | |
|
||
[![build status](https://img.shields.io/travis/lundegaard/validarium/master.svg?style=flat-square)](https://travis-ci.org/lundegaard/validarium) | ||
|
||
An agnostic validation library for JavaScript applications. | ||
An validation library for JavaScript applications. Ready to be used with react-intl. | ||
<br /> | ||
<br /> | ||
|
||
[`Try It`](https://runkit.com/aizerin/validarium) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
## Features | ||
|
||
This library provides an easy way how to validate your inputs in JavaScript. It provides three main packages. | ||
|
@@ -26,7 +28,11 @@ This library provides an easy way how to validate your inputs in JavaScript. It | |
|
||
## Installation | ||
|
||
Install desired packages with npm as follows | ||
Install desired packages | ||
|
||
`yarn add @validarium/core @validarium/intl` | ||
|
||
or | ||
|
||
`npm i @validarium/core @validarium/intl` | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,86 @@ | ||
{ | ||
"private": true, | ||
"version": "0.1.0", | ||
"license": "UNLICENSED", | ||
"main": "src/index.js", | ||
"description": "Validations with Ramda done right.", | ||
"contributors": [ | ||
"Tomas Konrady <tomas.konrady@lundegaard.eu>", | ||
"Vaclav Jancarik <vaclav.jancarik@lundegaard.eu>", | ||
"Kristyna Kamenicka <kristyna.kamenicka@lundegaard.eu>", | ||
"Lukas Sulik <lukas.sulik@lundegaard.eu>" | ||
], | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"workspaces": [ | ||
"packages/*" | ||
], | ||
"devDependencies": { | ||
"@babel/core": "^7.1.2", | ||
"@babel/plugin-proposal-class-properties": "^7.1.0", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.0.0", | ||
"@babel/plugin-transform-modules-commonjs": "^7.1.0", | ||
"@babel/plugin-transform-runtime": "^7.1.0", | ||
"@babel/preset-env": "^7.1.0", | ||
"babel-core": "^7.0.0-bridge", | ||
"babel-eslint": "^10.0.1", | ||
"babel-jest": "^23.6.0", | ||
"babel-plugin-ramda": "^1.6.3", | ||
"babel-preset-react-union": "^0.9.0", | ||
"cross-env": "^5.2.0", | ||
"eslint": "5.5.0", | ||
"eslint-config-react-union": "^0.7.2", | ||
"eslint-plugin-babel": "5.2.1", | ||
"eslint-plugin-import": "2.14.0", | ||
"husky": "1.1.2", | ||
"invariant": "^2.2.4", | ||
"jest": "^23.6.0", | ||
"lerna": "2.2.0", | ||
"lint-staged": "7.3.0", | ||
"prettier": "1.14.3", | ||
"ramda": "^0.25.0", | ||
"ramda-extension": "^0.6.1", | ||
"rollup": "^0.66.6", | ||
"rollup-plugin-auto-external": "^2.0.0", | ||
"rollup-plugin-babel": "^4.0.3", | ||
"rollup-plugin-commonjs": "^9.2.0", | ||
"rollup-plugin-node-resolve": "^3.4.0", | ||
"rollup-plugin-replace": "^2.1.0", | ||
"rollup-plugin-terser": "^3.0.0", | ||
"jsdoc-to-markdown": "^4.0.1" | ||
}, | ||
"scripts": { | ||
"publishAll": "yarn build && yarn lerna publish --access public --registry https://registry.npmjs.org/", | ||
"test": "jest", | ||
"lint": "yarn lint:eslint", | ||
"lint:eslint": "eslint --ext .js ./", | ||
"build": "lerna exec -- rollup -c=../../rollup.config.js", | ||
"prepublish": "yarn build", | ||
"generate:docs": "jsdoc2md -t ./packages/intl/scripts/docs.hbs --separators ./packages/intl/src/*.js > ./packages/intl/README.md" | ||
}, | ||
"lint-staged": { | ||
"linters": { | ||
"**/*.js": [ | ||
"prettier --ignore-path .gitignore --write", | ||
"yarn lint --fix", | ||
"git add" | ||
] | ||
} | ||
} | ||
"private": true, | ||
"version": "0.1.0", | ||
"license": "UNLICENSED", | ||
"main": "src/index.js", | ||
"description": "Validations with Ramda done right.", | ||
"contributors": [ | ||
"Tomas Konrady <tomas.konrady@lundegaard.eu>", | ||
"Vaclav Jancarik <vaclav.jancarik@lundegaard.eu>", | ||
"Kristyna Kamenicka <kristyna.kamenicka@lundegaard.eu>", | ||
"Lukas Sulik <lukas.sulik@lundegaard.eu>" | ||
], | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"workspaces": [ | ||
"packages/*" | ||
], | ||
"devDependencies": { | ||
"@babel/core": "^7.3.4", | ||
"@babel/plugin-proposal-class-properties": "^7.3.4", | ||
"@babel/plugin-proposal-decorators": "^7.3.0", | ||
"@babel/plugin-proposal-do-expressions": "^7.2.0", | ||
"@babel/plugin-proposal-export-default-from": "^7.2.0", | ||
"@babel/plugin-proposal-export-namespace-from": "^7.2.0", | ||
"@babel/plugin-proposal-function-sent": "^7.2.0", | ||
"@babel/plugin-proposal-logical-assignment-operators": "^7.2.0", | ||
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.2.0", | ||
"@babel/plugin-proposal-numeric-separator": "^7.2.0", | ||
"@babel/plugin-proposal-object-rest-spread": "^7.3.4", | ||
"@babel/plugin-proposal-optional-chaining": "^7.2.0", | ||
"@babel/plugin-proposal-pipeline-operator": "^7.3.2", | ||
"@babel/plugin-proposal-throw-expressions": "^7.2.0", | ||
"@babel/plugin-syntax-dynamic-import": "^7.2.0", | ||
"@babel/plugin-syntax-import-meta": "^7.2.0", | ||
"@babel/plugin-transform-modules-commonjs": "^7.2.0", | ||
"@babel/plugin-transform-react-constant-elements": "^7.2.0", | ||
"@babel/plugin-transform-runtime": "^7.3.4", | ||
"@babel/preset-env": "^7.3.4", | ||
"@babel/preset-react": "^7.0.0", | ||
"babel-core": "^7.0.0-bridge", | ||
"babel-eslint": "^10.0.1", | ||
"babel-jest": "^24.1.0", | ||
"babel-plugin-ramda": "^2.0.0", | ||
"babel-preset-react-union": "^0.12.0", | ||
"cross-env": "^5.2.0", | ||
"eslint": "5.15.0", | ||
"eslint-config-react-union": "^0.12.0", | ||
"eslint-plugin-babel": "5.3.0", | ||
"eslint-plugin-import": "2.16.0", | ||
"husky": "1.3.1", | ||
"invariant": "^2.2.4", | ||
"jest": "^24.1.0", | ||
"jsdoc-to-markdown": "^4.0.1", | ||
"lerna": "2.2.0", | ||
"lint-staged": "8.1.5", | ||
"prettier": "1.16.4", | ||
"ramda": "^0.26.1", | ||
"ramda-extension": "^0.7.2", | ||
"rollup": "^1.4.1", | ||
"rollup-plugin-auto-external": "^2.0.0", | ||
"rollup-plugin-babel": "^4.3.2", | ||
"rollup-plugin-commonjs": "^9.2.1", | ||
"rollup-plugin-node-resolve": "^4.0.1", | ||
"rollup-plugin-replace": "^2.1.0", | ||
"rollup-plugin-terser": "^4.0.4" | ||
}, | ||
"scripts": { | ||
"publishAll": "yarn build && yarn lerna publish --access public --registry https://registry.npmjs.org/", | ||
"test": "jest", | ||
"lint": "yarn lint:eslint", | ||
"lint:eslint": "eslint --ext .js ./", | ||
"build": "lerna exec -- rollup -c=../../rollup.config.js", | ||
"prepublish": "yarn build", | ||
"generate:docs": "node scripts/generateDocs.js" | ||
}, | ||
"lint-staged": { | ||
"linters": { | ||
"**/*.js": [ | ||
"prettier --ignore-path .gitignore --write", | ||
"yarn lint --fix", | ||
"git add" | ||
] | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,159 @@ | ||
# Core | ||
|
||
This package contains all core functionalities for Validarium. | ||
|
||
## API reference | ||
<a name="module_core"></a> | ||
|
||
## core | ||
|
||
* [core](#module_core) | ||
* [.validate](#module_core.validate) | ||
* [.validateMany](#module_core.validateMany) | ||
* [.createMainValidate](#module_core.createMainValidate) ⇒ <code>Object</code> | ||
* [.createOptionalValidation(fn, react, params)](#module_core.createOptionalValidation) ⇒ <code>Object</code> \| <code>null</code> | ||
* [.createValidation(fn, react, params)](#module_core.createValidation) ⇒ <code>Object</code> \| <code>null</code> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that there shoud be |
||
|
||
|
||
* * * | ||
|
||
<a name="module_core.validate"></a> | ||
|
||
### core.validate | ||
Applies validations in `descriptor` for `value`. | ||
Params are curried. | ||
|
||
**Sig**: Object -> a -> b | ||
|
||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| descriptor | <code>object</code> | Object that contains validations for each item in `values` | | ||
| value | <code>any</code> | Value for validation | | ||
|
||
**Example** | ||
```js | ||
validate({ | ||
id: [(x) => !x && 'Is required.', (x) => x < 0 && 'Must be greater than 0.'], | ||
name: [(x) => !x && 'Is required.'], | ||
surname: [(x) => !x && 'Is required.'], | ||
}, { | ||
id: -1, | ||
surname: 'Doe', | ||
} | ||
]) | ||
// Output: | ||
// { | ||
// id: 'Must be greater than 0.', | ||
// name: 'Is required.', | ||
// surname: false, | ||
// } | ||
``` | ||
|
||
* * * | ||
|
||
<a name="module_core.validateMany"></a> | ||
|
||
### core.validateMany | ||
Applies validations in `descriptor` for each item in `values`. | ||
Params are curried. | ||
|
||
**Sig**: Object -> [Object] -> [Object] | ||
|
||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| descriptor | <code>object</code> | Object that contains validations for each item in `values` | | ||
| values | <code>array</code> | Values for validations | | ||
|
||
**Example** | ||
```js | ||
validateMany({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you please adjust the formatting of all examples (indentation in particular)? |
||
id: [(x) => !x && 'Is required.', (x) => x < 0 && 'Must be greater than 0.'], | ||
name: [(x) => !x && 'Is required.'], | ||
surname: [(x) => !x && 'Is required.'], | ||
}, [ | ||
{ | ||
id: -1, | ||
surname: 'Doe', | ||
}, | ||
{ | ||
id: 13, | ||
name: 'Bob', | ||
}, | ||
]) | ||
// Output: | ||
// [ | ||
// { | ||
// id: 'Must be greater than 0.', | ||
// name: 'Is required.', | ||
// surname: false, | ||
// }, { | ||
// id: false, | ||
// name: false, | ||
// surname: 'Is required.', | ||
// }, | ||
// ] | ||
``` | ||
|
||
* * * | ||
|
||
<a name="module_core.createMainValidate"></a> | ||
|
||
### core.createMainValidate ⇒ <code>Object</code> | ||
Applies `fns` with `values` and `props`. | ||
Results of `fns` are translated with `react-intl`; | ||
We assume that `props` contains `intl` object from `react-intl` (usually obtained by `injectIntl`). | ||
|
||
**Returns**: <code>Object</code> - Translated result. | ||
|
||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| ...fns | <code>function</code> | Validation functions. | | ||
| props | <code>Object</code> | | | ||
| values | <code>any</code> | | | ||
|
||
|
||
* * * | ||
|
||
<a name="module_core.createOptionalValidation"></a> | ||
|
||
### core.createOptionalValidation(fn, react, params) ⇒ <code>Object</code> \| <code>null</code> | ||
Creates optional validation function with predicate and message | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think that there shoud be |
||
Results of validation is ready for translation by react-intl. | ||
|
||
**Returns**: <code>Object</code> \| <code>null</code> - Message object when fails { message, messageValues } or null if pass | ||
|
||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| fn | <code>function</code> | validation predicate. | | ||
| react | <code>String</code> | intl message (eg. { id: 1, defaultMessage: '' }) | | ||
| params | <code>Object</code> | for intl message (eg. { min: 1, max: 2 }) | | ||
|
||
|
||
* * * | ||
|
||
<a name="module_core.createValidation"></a> | ||
|
||
### core.createValidation(fn, react, params) ⇒ <code>Object</code> \| <code>null</code> | ||
Creates mandatory validation function with predicate and message | ||
Results of validation is ready for translation by react-intl. | ||
|
||
**Returns**: <code>Object</code> \| <code>null</code> - Message object when fails { message, messageValues } or null if pass | ||
|
||
| Param | Type | Description | | ||
| --- | --- | --- | | ||
| fn | <code>function</code> | validation predicate. | | ||
| react | <code>String</code> | intl message (eg. { id: 1, defaultMessage: '' }) | | ||
| params | <code>Object</code> | for intl message (eg. { min: 1, max: 2 }) | | ||
|
||
**Example** | ||
```js | ||
> const hasLength = length => createValidation(hasLength(length), m.hasLength, { length }) | ||
> hasLength(6)('abcdef') | ||
null | ||
``` | ||
|
||
* * * | ||
|
||
|
||
[back to main page](../../README.md) | ||
|
||
© 2018 Lundegaard a.s. | ||
© 2018-2019 Lundegaard a.s. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
import { always, ifElse } from 'ramda'; | ||
import { alwaysNull } from 'ramda-extension'; | ||
|
||
/** | ||
* Creates optional validation function with predicate and message | ||
* Results of validation is ready for translation by react-intl. | ||
* | ||
* @param {Function} fn validation predicate. | ||
* @param {String} react intl message (eg. { id: 1, defaultMessage: '' }) | ||
* @param {Object} params for intl message (eg. { min: 1, max: 2 }) | ||
* @return {Object|null} Message object when fails { message, messageValues } or null if pass | ||
* | ||
* @alias module:core.createOptionalValidation | ||
*/ | ||
export default (predicate, message, messageValues) => | ||
ifElse(predicate, alwaysNull, always({ message, messageValues })); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, it is not agnostic yet... but it is definitely our goal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will create issue