Skip to content

Commit

Permalink
Merge pull request #52 from web-pyjs/costum
Browse files Browse the repository at this point in the history
Add Costum rule
  • Loading branch information
nsoufian authored Feb 8, 2019
2 parents a96697a + 03f99c6 commit 97b96c8
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 1 deletion.
25 changes: 25 additions & 0 deletions docs/api-reference/rules/generic.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Generic are rules that you can apply besides any type
## Methods

- [required([options])](#required-options)
- [custom([options])](#custom-options)
- [none([options])](#none-options)
- [empty([options])](#empty-options)
- [equals(value, [options])](#equalsvalue-options)
Expand Down Expand Up @@ -54,6 +55,30 @@ field.validate(null); // true
field.validate(9); // false, if the Field is optional and is not empty other rules are checked
```

### custom([options])

Pass a custom validator function to check against the value of the field.

Optionally, you can provide a options object that can hold **message** and **constraints**.

> **NOTE :** : validator function must return **True** or **False**.
Example :

```javascript
import { Field } from "v4f";

const checkUppercase = value => value === value.toUpperCase();

const field = Field()
.string()
.custom(checkUppercase)
.required();

field.validate("STR"); // true
field.validate("str"); // false
```

### none([options])

Checks if the field value is a **null** or **undefined** or empty **string**, **object**, an **array**.
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type Rules<T, R = T> = {
not: Rules<T, R>;
equals(value: any | RelatedField, options?: Options): Rules<T, R>;
exact(value: any | RelatedField, options?: Options): Rules<T, R>;
custom(value: any | RelatedField, options?: Options): Rules<T, R>;
none(options?: Options): Rules<T, R>;
empty(options?: Options): Rules<T, R>;
oneOf(
Expand Down
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
{
"name": "v4f",
"version": "0.1.1",
"version": "0.1.2",
"homepage": "https://v4f.js.org",
"repository": {
"type": "git",
"url": "https://github.com/web-pyjs/v4f"
},
"description": "A declarative, efficient, and flexible JavaScript validation library for Humans .",
"maintainers": [
"soufiane nassih"
Expand Down
2 changes: 2 additions & 0 deletions src/rules/generic.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export const required = value =>
isEmpty(value) !== true &&
(value instanceof Array ? value.length !== 0 : true);

export const custom = (fun, value) => fun(value);

export const equals = (equalsValue, value) =>
typeof value === "object" && typeof equalsValue === "object"
? isObjectsEquals(value, equalsValue)
Expand Down
28 changes: 28 additions & 0 deletions src/tests/field.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,31 @@ describe("Validate not Required in Array Field", () => {
});
});
});

describe("Validate Custom rule with uppercase in String Field", () => {
const values = { valid: ["UP", "IP", "LOB"], invalid: ["lll", "il"] };

const customFun = v => {
if (v === v.toUpperCase()) {
return true;
}
return false;
};

const rule = v =>
Field()
.string()
.custom(customFun)
.validate(v);

values.valid.forEach(v => {
it(`Value : ${v} , should be true`, () => {
expect(rule(v)).toBe(true);
});
});
values.invalid.forEach(v => {
it(`Value : ${v} , should be false`, () => {
expect(rule(v)).toBe(false);
});
});
});
19 changes: 19 additions & 0 deletions src/tests/generic.rules.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
empty,
none,
oneOf,
custom,
exactOneOf
} from "../rules/generic";

Expand Down Expand Up @@ -175,3 +176,21 @@ describe("Validate oneOf Rule with 4 as value", () => {
});
});
});

describe("Validate Custom Rule", () => {
const values = {
valid: [{ fun: v => v === true, v: true }, { fun: v => v >= 5, v: 6 }],
invalid: [{ fun: v => v === true, v: false }, { fun: v => v >= 5, v: 3 }]
};
const rule = custom;
values.valid.forEach(({ v, fun }) => {
it(`Value : ${v} , should be true`, () => {
expect(rule(fun, v)).toBe(true);
});
});
values.invalid.forEach(({ v, fun }) => {
it(`Value : ${v} , should be false`, () => {
expect(rule(fun, v)).toBe(false);
});
});
});

0 comments on commit 97b96c8

Please sign in to comment.