validx
Advanced tools
Comparing version 0.0.4 to 0.1.0
@@ -69,2 +69,4 @@ import { IObservableArray } from 'mobx'; | ||
}): this; | ||
getErrors(field: string): string[]; | ||
getError(field: string): string | undefined; | ||
} | ||
@@ -76,2 +78,4 @@ /** | ||
validate(schema: IValidationSchema<T>): this; | ||
getErrors(field: keyof T): string[]; | ||
getError(field: keyof T): string | undefined; | ||
} | ||
@@ -81,3 +85,3 @@ /** | ||
*/ | ||
export interface ISchemaBoundValidationContext extends IValidationContext { | ||
export interface ISchemaBoundValidationContext<T> extends IBoundValidationContext<T> { | ||
validate(): this; | ||
@@ -94,14 +98,2 @@ } | ||
/** | ||
* Initializes a new instance of ValidationContext. | ||
*/ | ||
constructor(); | ||
/** | ||
* Internal map of the errors. | ||
* | ||
* @private | ||
* | ||
* @memberOf ValidationContext | ||
*/ | ||
private errorsMap; | ||
/** | ||
* All validation errors are stored here. To clear, call `reset`. | ||
@@ -123,2 +115,14 @@ * | ||
/** | ||
* Internal map of the errors. | ||
* | ||
* @private | ||
* | ||
* @memberOf ValidationContext | ||
*/ | ||
private errorsMap; | ||
/** | ||
* Initializes a new instance of ValidationContext. | ||
*/ | ||
constructor(); | ||
/** | ||
* Resets the errors. | ||
@@ -150,2 +154,11 @@ * | ||
/** | ||
* Gets the errors for the given field. | ||
*/ | ||
getErrors(field: string): string[]; | ||
/** | ||
* Gets the first error for the given field. | ||
* If not found, returns undefined. | ||
*/ | ||
getError(field: string): string; | ||
/** | ||
* Ensures that an entry in the internal error map | ||
@@ -172,4 +185,4 @@ * exists for the specified field. | ||
} | ||
export declare function validationContext<T>(objectToValidate: T, schema: IValidationSchema<T>): ISchemaBoundValidationContext; | ||
export declare function validationContext<T>(objectToValidate: T, schema: IValidationSchema<T>): ISchemaBoundValidationContext<T>; | ||
export declare function validationContext<T>(objectToValidate: T): IBoundValidationContext<T>; | ||
export declare function validationContext(): IValidationContext; |
"use strict"; | ||
var mobx_1 = require("mobx"); | ||
var every = require('lodash/every'); | ||
var forEach = require('lodash/forEach'); | ||
var utils_1 = require("./utils"); | ||
/** | ||
@@ -27,3 +26,3 @@ * Implementation of the validation context. | ||
isValid: mobx_1.computed(function () { | ||
return every(_this.errors, function (arr) { return arr.length === 0; }); | ||
return utils_1.every(_this.errors, function (arr) { return arr.length === 0; }); | ||
}) | ||
@@ -56,6 +55,6 @@ }); | ||
var _this = this; | ||
forEach(schema, function (validators, field) { | ||
utils_1.forEach(schema, function (validators, field) { | ||
var errors = _this.ensureErrors(field); | ||
var value = obj[field]; | ||
forEach(validators, function (validator) { | ||
utils_1.forEach(validators, function (validator) { | ||
var opts = { | ||
@@ -85,3 +84,3 @@ field: field, | ||
var _this = this; | ||
forEach(errors, function (arr, field) { | ||
utils_1.forEach(errors, function (arr, field) { | ||
(_a = _this.ensureErrors(field)).push.apply(_a, arr); | ||
@@ -93,2 +92,19 @@ var _a; | ||
/** | ||
* Gets the errors for the given field. | ||
*/ | ||
ValidationContext.prototype.getErrors = function (field) { | ||
var errors = this.errors[field]; | ||
if (!errors) { | ||
return []; | ||
} | ||
return errors.slice(); | ||
}; | ||
/** | ||
* Gets the first error for the given field. | ||
* If not found, returns undefined. | ||
*/ | ||
ValidationContext.prototype.getError = function (field) { | ||
return this.getErrors(field)[0]; | ||
}; | ||
/** | ||
* Ensures that an entry in the internal error map | ||
@@ -95,0 +111,0 @@ * exists for the specified field. |
{ | ||
"name": "validx", | ||
"version": "0.0.4", | ||
"version": "0.1.0", | ||
"description": "Collection + Model library for MobX", | ||
@@ -14,4 +14,4 @@ "main": "lib/index.js", | ||
"test:watch": "nodemon -e js,ts --exec npm run test", | ||
"lint": "tslint lib/ test/", | ||
"lint:watch": "nodemon --exec npm run lint", | ||
"lint": "tslint 'src/**/*.ts' 'test/**/*.ts'", | ||
"lint:watch": "nodemon -e ts --exec npm run lint", | ||
"cover": "nyc npm test", | ||
@@ -35,4 +35,3 @@ "coveralls": "nyc report --reporter=text-lcov | coveralls", | ||
"mobx", | ||
"backbone", | ||
"tfrp", | ||
"validation", | ||
"state management", | ||
@@ -62,2 +61,3 @@ "react" | ||
"tslint": "^4.4.2", | ||
"tslint-config-standard": "^4.0.0", | ||
"typescript": "2.2.0" | ||
@@ -67,5 +67,4 @@ }, | ||
"email-validator": "^1.0.7", | ||
"lodash": "^4.17.4", | ||
"mobx": "^3.1.0" | ||
} | ||
} |
@@ -12,3 +12,3 @@ # validx | ||
Validation library using MobX. | ||
Validation library for [MobX](https://github.com/mobxjs/mobx). | ||
@@ -26,4 +26,4 @@ ## Install | ||
**ValidX** is built for MobX and is easy to use, yet powerful enough to add any validation | ||
you'd like. | ||
**ValidX** is built for [MobX](https://github.com/mobxjs/mobx) and is easy to use, yet | ||
powerful enough to add any validation you'd like. | ||
@@ -59,3 +59,3 @@ # Examples | ||
Calling it will populate the `errors` property with any validation errors | ||
Calling it will populate the `errors` property with any validation errors | ||
found on the object | ||
@@ -96,6 +96,10 @@ | ||
}, schema) | ||
``` | ||
// Now that we have validated our object, we can pull the errors | ||
// from the context. | ||
console.log(validation.isValid) | ||
Now that we have validated our object, we can pull the errors | ||
from the context. | ||
```js | ||
console.log(validation.isValid) | ||
// false | ||
@@ -108,4 +112,7 @@ | ||
// ['Email is required', 'Not a valid email'] | ||
``` | ||
// To validate again, we need to reset the context and then validate. | ||
To validate again, we need to reset the context and then call validate. | ||
```js | ||
validation.reset().validate({ | ||
@@ -116,3 +123,3 @@ name: 'Jeff', | ||
console.log(validation.isValid) | ||
console.log(validation.isValid) | ||
// false | ||
@@ -125,5 +132,8 @@ | ||
// ['Not a valid email'] | ||
``` | ||
// Let's see what the errors are like when we | ||
// log them after resetting. | ||
Let's see what the errors are like when we | ||
log them after resetting. | ||
```js | ||
validation.reset() | ||
@@ -137,6 +147,8 @@ console.log(validation.isValid) | ||
// undefined | ||
``` | ||
// They are undefined because we don't know | ||
// what fields will be validated yet. | ||
They are undefined because we don't know | ||
what fields will be validated yet. | ||
```js | ||
validation.validate({ | ||
@@ -147,3 +159,3 @@ name: 'Jeff', | ||
console.log(validation.isValid) | ||
console.log(validation.isValid) | ||
// false | ||
@@ -160,3 +172,3 @@ | ||
Resets the internal state of the context. You usually use this before | ||
Resets the internal state of the context. You usually use this before | ||
starting a new validation. `reset()` is a MobX `action`. | ||
@@ -181,5 +193,5 @@ | ||
@observable label = '' | ||
validation = validationContext(this) | ||
@action validate () { | ||
@@ -194,3 +206,3 @@ this.validation.validate({ | ||
@observable placeholder = '' | ||
@action validate () { | ||
@@ -236,2 +248,36 @@ // reset before caling super | ||
## `getErrors()` | ||
Safer way to get errors for a field rather than using `errors.field`, | ||
as this will return an empty array in case there are no errors. | ||
```js | ||
const validation = validationContext() | ||
validation.addErrors({ name: ['Not cool'] }) | ||
validation.getErrors('name') | ||
// ['Not cool'] | ||
validation.reset() | ||
validation.getErrors('name') | ||
// [] | ||
``` | ||
## `getError()` | ||
Convenience method for `getErrors('field')[0]`. | ||
Returns `undefined` if the error is not found. | ||
```js | ||
const validation = validationContext() | ||
validation.addErrors({ name: ['Not cool'] }) | ||
validation.getError('name') | ||
// 'Not cool' | ||
validation.reset() | ||
validation.getError('name') | ||
// undefined | ||
``` | ||
## `errors` | ||
@@ -333,2 +379,14 @@ | ||
# Changelog | ||
## v0.1.0 | ||
- Added `validation.getErrors()` | ||
- Added `validation.getError()` | ||
- Removed `lodash` dependency | ||
## v0.0.4 | ||
- First official release. | ||
# Author | ||
@@ -335,0 +393,0 @@ |
Sorry, the diff of this file is not supported yet
31071
2
18
474
384
16
- Removedlodash@^4.17.4
- Removedlodash@4.17.21(transitive)