Skip to content

Commit 97b96c8

Browse files
authored
Merge pull request #52 from web-pyjs/costum
Add Costum rule
2 parents a96697a + 03f99c6 commit 97b96c8

File tree

6 files changed

+81
-1
lines changed

6 files changed

+81
-1
lines changed

docs/api-reference/rules/generic.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Generic are rules that you can apply besides any type
88
## Methods
99

1010
- [required([options])](#required-options)
11+
- [custom([options])](#custom-options)
1112
- [none([options])](#none-options)
1213
- [empty([options])](#empty-options)
1314
- [equals(value, [options])](#equalsvalue-options)
@@ -54,6 +55,30 @@ field.validate(null); // true
5455
field.validate(9); // false, if the Field is optional and is not empty other rules are checked
5556
```
5657

58+
### custom([options])
59+
60+
Pass a custom validator function to check against the value of the field.
61+
62+
Optionally, you can provide a options object that can hold **message** and **constraints**.
63+
64+
> **NOTE :** : validator function must return **True** or **False**.
65+
66+
Example :
67+
68+
```javascript
69+
import { Field } from "v4f";
70+
71+
const checkUppercase = value => value === value.toUpperCase();
72+
73+
const field = Field()
74+
.string()
75+
.custom(checkUppercase)
76+
.required();
77+
78+
field.validate("STR"); // true
79+
field.validate("str"); // false
80+
```
81+
5782
### none([options])
5883

5984
Checks if the field value is a **null** or **undefined** or empty **string**, **object**, an **array**.

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Rules<T, R = T> = {
1010
not: Rules<T, R>;
1111
equals(value: any | RelatedField, options?: Options): Rules<T, R>;
1212
exact(value: any | RelatedField, options?: Options): Rules<T, R>;
13+
custom(value: any | RelatedField, options?: Options): Rules<T, R>;
1314
none(options?: Options): Rules<T, R>;
1415
empty(options?: Options): Rules<T, R>;
1516
oneOf(

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
{
22
"name": "v4f",
3-
"version": "0.1.1",
3+
"version": "0.1.2",
4+
"homepage": "https://v4f.js.org",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/web-pyjs/v4f"
8+
},
49
"description": "A declarative, efficient, and flexible JavaScript validation library for Humans .",
510
"maintainers": [
611
"soufiane nassih"

src/rules/generic.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export const required = value =>
1414
isEmpty(value) !== true &&
1515
(value instanceof Array ? value.length !== 0 : true);
1616

17+
export const custom = (fun, value) => fun(value);
18+
1719
export const equals = (equalsValue, value) =>
1820
typeof value === "object" && typeof equalsValue === "object"
1921
? isObjectsEquals(value, equalsValue)

src/tests/field.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,31 @@ describe("Validate not Required in Array Field", () => {
9292
});
9393
});
9494
});
95+
96+
describe("Validate Custom rule with uppercase in String Field", () => {
97+
const values = { valid: ["UP", "IP", "LOB"], invalid: ["lll", "il"] };
98+
99+
const customFun = v => {
100+
if (v === v.toUpperCase()) {
101+
return true;
102+
}
103+
return false;
104+
};
105+
106+
const rule = v =>
107+
Field()
108+
.string()
109+
.custom(customFun)
110+
.validate(v);
111+
112+
values.valid.forEach(v => {
113+
it(`Value : ${v} , should be true`, () => {
114+
expect(rule(v)).toBe(true);
115+
});
116+
});
117+
values.invalid.forEach(v => {
118+
it(`Value : ${v} , should be false`, () => {
119+
expect(rule(v)).toBe(false);
120+
});
121+
});
122+
});

src/tests/generic.rules.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
empty,
66
none,
77
oneOf,
8+
custom,
89
exactOneOf
910
} from "../rules/generic";
1011

@@ -175,3 +176,21 @@ describe("Validate oneOf Rule with 4 as value", () => {
175176
});
176177
});
177178
});
179+
180+
describe("Validate Custom Rule", () => {
181+
const values = {
182+
valid: [{ fun: v => v === true, v: true }, { fun: v => v >= 5, v: 6 }],
183+
invalid: [{ fun: v => v === true, v: false }, { fun: v => v >= 5, v: 3 }]
184+
};
185+
const rule = custom;
186+
values.valid.forEach(({ v, fun }) => {
187+
it(`Value : ${v} , should be true`, () => {
188+
expect(rule(fun, v)).toBe(true);
189+
});
190+
});
191+
values.invalid.forEach(({ v, fun }) => {
192+
it(`Value : ${v} , should be false`, () => {
193+
expect(rule(fun, v)).toBe(false);
194+
});
195+
});
196+
});

0 commit comments

Comments
 (0)