Comparing version 1.1.0 to 1.1.1
@@ -0,1 +1,4 @@ | ||
# 1.1.1 | ||
* If a validator dies unexpectedly (throws an error/exception) the `validate` call does the same instead of silently ignoring it. Should not cause any change if your validators worked previously. | ||
# 1.1.0 | ||
@@ -2,0 +5,0 @@ * Added support for `Map` in `FormState` class. |
# Setup | ||
`npm install` | ||
# Run Demos | ||
# Run Demos / Tests | ||
* `npm start` | ||
* visit http://localhost:4000/demos/ | ||
* Edit `src/scripts/demos.ts` | ||
* Visit http://localhost:4000/demos/ Edit `src/scripts/demos.ts` | ||
* Edit tests, see terminal for results | ||
@@ -9,0 +9,0 @@ # Write Docs |
/** A truthy string or falsy values */ | ||
export declare type ValidationResponse = string | null | undefined | false; | ||
/** The return value of a validator */ | ||
export declare type ValidatorResponse = ValidationResponse | Promise<ValidationResponse>; | ||
/** | ||
@@ -8,6 +10,8 @@ * A validator simply takes a value and returns a string or Promise<string> | ||
export interface Validator<TValue> { | ||
(value: TValue): ValidationResponse | Promise<ValidationResponse>; | ||
(value: TValue): ValidatorResponse; | ||
} | ||
/** | ||
* Runs the value through a list of validators. As soon as a validation error is detected, the error is returned | ||
* Runs the value through a list of validators. | ||
* - As soon as a validation error is detected, the error is returned | ||
* - As soon as a validator dies unexpectedly (throws an error), we throw the same error. | ||
*/ | ||
@@ -14,0 +18,0 @@ export declare function applyValidators<TValue>(value: TValue, validators: Validator<TValue>[]): Promise<string | null | undefined>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var utils_1 = require("../internal/utils"); | ||
/** | ||
* Runs the value through a list of validators. As soon as a validation error is detected, the error is returned | ||
* Runs the value through a list of validators. | ||
* - As soon as a validation error is detected, the error is returned | ||
* - As soon as a validator dies unexpectedly (throws an error), we throw the same error. | ||
*/ | ||
function applyValidators(value, validators) { | ||
return new Promise(function (resolve) { | ||
return new Promise(function (resolve, reject) { | ||
var currentIndex = 0; | ||
@@ -26,13 +29,15 @@ var gotoNextValidator = function () { | ||
// some error | ||
if (!res.then) { | ||
if (!utils_1.isPromiseLike(res)) { | ||
resolve(res); | ||
return; | ||
} | ||
// wait for error response | ||
// wait for validator response | ||
res.then(function (msg) { | ||
// no error | ||
if (!msg) | ||
gotoNextValidator(); | ||
// some error | ||
else | ||
resolve(msg); | ||
}); | ||
}).catch(reject); | ||
}; | ||
@@ -39,0 +44,0 @@ // kickoff |
export declare function debounce<T extends Function>(func: T, milliseconds: number, immediate?: boolean): T; | ||
export declare function isMapLike(thing: any): boolean; | ||
export declare function isPromiseLike(arg: any): arg is Promise<any>; |
@@ -41,3 +41,2 @@ "use strict"; | ||
exports.debounce = debounce; | ||
; | ||
function isMapLike(thing) { | ||
@@ -48,1 +47,5 @@ return mobx_1.isObservableMap(thing) | ||
exports.isMapLike = isMapLike; | ||
function isPromiseLike(arg) { | ||
return arg != null && typeof arg === "object" && typeof arg.then === "function"; | ||
} | ||
exports.isPromiseLike = isPromiseLike; |
{ | ||
"name": "formstate", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Painless and simple MobX form management", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -1,4 +0,3 @@ | ||
import * as utils from '../internal/utils'; | ||
import { isPromiseLike } from "../internal/utils" | ||
/** A truthy string or falsy values */ | ||
@@ -11,2 +10,7 @@ export type ValidationResponse = | ||
/** The return value of a validator */ | ||
export type ValidatorResponse = | ||
ValidationResponse | ||
| Promise<ValidationResponse> | ||
/** | ||
@@ -17,10 +21,12 @@ * A validator simply takes a value and returns a string or Promise<string> | ||
export interface Validator<TValue> { | ||
(value: TValue): ValidationResponse | Promise<ValidationResponse>; | ||
(value: TValue): ValidatorResponse; | ||
} | ||
/** | ||
* Runs the value through a list of validators. As soon as a validation error is detected, the error is returned | ||
* Runs the value through a list of validators. | ||
* - As soon as a validation error is detected, the error is returned | ||
* - As soon as a validator dies unexpectedly (throws an error), we throw the same error. | ||
*/ | ||
export function applyValidators<TValue>(value: TValue, validators: Validator<TValue>[]): Promise<string | null | undefined> { | ||
return new Promise<string | null | undefined>(resolve => { | ||
return new Promise<string | null | undefined>((resolve, reject) => { | ||
let currentIndex = 0; | ||
@@ -39,3 +45,3 @@ | ||
let validator = validators[currentIndex]; | ||
let res: any = validator(value); | ||
let res = validator(value); | ||
@@ -49,3 +55,3 @@ // no error | ||
// some error | ||
if (!res.then) { | ||
if (!isPromiseLike(res)) { | ||
resolve(res); | ||
@@ -55,7 +61,9 @@ return; | ||
// wait for error response | ||
// wait for validator response | ||
res.then((msg: any) => { | ||
// no error | ||
if (!msg) gotoNextValidator(); | ||
// some error | ||
else resolve(msg); | ||
}) | ||
}).catch(reject) | ||
} | ||
@@ -62,0 +70,0 @@ |
@@ -39,3 +39,3 @@ import { isObservableMap } from "mobx" | ||
}; | ||
}; | ||
} | ||
@@ -46,1 +46,5 @@ export function isMapLike(thing: any) { | ||
} | ||
export function isPromiseLike(arg: any): arg is Promise<any> { | ||
return arg != null && typeof arg === "object" && typeof arg.then === "function"; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
0
76896
27
2015