Comparing version 0.2.4 to 0.2.5
# CHANGELOG | ||
## 0.2.5 | ||
* `isMinLength` can handle undefined | ||
## 0.2.4 | ||
@@ -4,0 +8,0 @@ |
@@ -1,7 +0,8 @@ | ||
export declare const isRequired: _.CurriedFunction2<(value: any) => string, string | boolean, string>; | ||
export declare const isMinLength: _.CurriedFunction3<number, (value: any) => string, string, string>; | ||
export declare const isMaxLength: _.CurriedFunction3<number, (value: any) => string, string, string>; | ||
export declare const isEqualLength: _.CurriedFunction3<number, (value: any) => string, string, string>; | ||
export declare const isEmail: _.CurriedFunction2<(value: any) => string, string, string>; | ||
export declare const isMatching: _.CurriedFunction3<RegExp, (value: any) => string, string, string>; | ||
export declare const isPassing: _.CurriedFunction3<(value: any) => boolean, (value: any) => string, string, string>; | ||
export declare type Message = (value: any) => string; | ||
export declare const isRequired: _.CurriedFunction2<Message, string | boolean, string>; | ||
export declare const isMinLength: _.CurriedFunction3<number, Message, string, string>; | ||
export declare const isMaxLength: _.CurriedFunction3<number, Message, string, string>; | ||
export declare const isEqualLength: _.CurriedFunction3<number, Message, string, string>; | ||
export declare const isEmail: _.CurriedFunction2<Message, string, string>; | ||
export declare const isMatching: _.CurriedFunction3<RegExp, Message, string, string>; | ||
export declare const isPassing: _.CurriedFunction3<(value: any) => boolean, Message, string, string>; |
@@ -8,3 +8,3 @@ "use strict"; | ||
exports.isMinLength = curry(function (min, message, value) { | ||
var valid = value.length >= min; | ||
var valid = value && value.length >= min; | ||
return valid ? null : message(value); | ||
@@ -11,0 +11,0 @@ }); |
{ | ||
"name": "policeman", | ||
"version": "0.2.4", | ||
"version": "0.2.5", | ||
"description": "Lightweight yet powerful schema validator", | ||
@@ -5,0 +5,0 @@ "main": "lib/policeman.js", |
@@ -31,3 +31,3 @@ # policeman | ||
// setup entry filter predicates | ||
const isGift: Filter = (value, source) => source.gift === true; | ||
const isGift = (value, source) => source.gift === true; | ||
@@ -40,2 +40,3 @@ // define schema | ||
// 4. skip validation based on filter predicate | ||
["email", "email", [requiredValidator, emailValidator]], // #1 | ||
@@ -45,2 +46,3 @@ ["phone", "phone", combineValidators(requiredValidator, phoneNumberValidator)], // #2 | ||
["giftCode", "giftCode", requiredValidator, isGift], // #4 | ||
// [dest, source, Validator, Filter] | ||
]; | ||
@@ -106,7 +108,7 @@ | ||
const isCreditCard = isPassing(validator.isCreditCard, () => "is invalid credit card"); | ||
const isUUID4 = isPassing(value => validator.isUUID(value, 4), () => "is invalid UUID v4"); | ||
const isFTP = isPassing(value => validator.isURL(value, { protocols: ["ftp"] }, () => "is invalid FTP address"); | ||
const creditCardValidator = isPassing(validator.isCreditCard, () => "is invalid credit card"); | ||
const uuid4Validator = isPassing(value => validator.isUUID(value, 4), () => "is invalid UUID v4"); | ||
const ftpValidator = isPassing(value => validator.isURL(value, { protocols: ["ftp"] }, () => "is invalid FTP address"); | ||
``` | ||
See [tests](src/test/validators.test.ts) for more examples. |
import * as curry from "lodash/curry"; | ||
type Message = (value: any) => string; | ||
export type Message = (value: any) => string; | ||
@@ -11,3 +11,3 @@ export const isRequired = curry((message: Message, value: string | boolean) => { | ||
export const isMinLength = curry((min: number, message: Message, value: string) => { | ||
const valid = value.length >= min; | ||
const valid = value && value.length >= min; | ||
return valid ? null : message(value); | ||
@@ -14,0 +14,0 @@ }); |
@@ -26,2 +26,3 @@ import * as tape from "tape"; | ||
t.is(isMinLength4("foo"), "should be at least 4", "isMinLength(4) fails on strings with 4- characters"); | ||
t.is(isMinLength4(undefined), "should be at least 4", "isMinLength(4) fails on undefined"); | ||
} | ||
@@ -65,3 +66,3 @@ | ||
tape("validators", (t: tape.Test) => { | ||
t.plan(22); | ||
t.plan(23); | ||
testIsRequired(t); | ||
@@ -68,0 +69,0 @@ testIsMinLengthValidator(t); |
@@ -17,2 +17,3 @@ "use strict"; | ||
t.is(isMinLength4("foo"), "should be at least 4", "isMinLength(4) fails on strings with 4- characters"); | ||
t.is(isMinLength4(undefined), "should be at least 4", "isMinLength(4) fails on undefined"); | ||
} | ||
@@ -50,3 +51,3 @@ function testIsMaxLengthValidator(t) { | ||
tape("validators", function (t) { | ||
t.plan(22); | ||
t.plan(23); | ||
testIsRequired(t); | ||
@@ -53,0 +54,0 @@ testIsMinLengthValidator(t); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
715711
38
21526
111