Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

next : isNegative & isPositive feature implementation #433

Merged
merged 5 commits into from Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions packages/n4s/src/rules/index.js
Expand Up @@ -5,7 +5,7 @@ import greaterThan from './greaterThan';
import greaterThanOrEquals from './greaterThanOrEquals';
import inside from './inside';
import isArray from './isArray';
import isBetween from './isBetween'
import isBetween from './isBetween';
import isEmpty from './isEmpty';
import isEven from './isEven';
import isNaN from './isNaN';
Expand All @@ -26,6 +26,7 @@ import numberEquals from './numberEquals';
import shorterThan from './shorterThan';
import shorterThanOrEquals from './shorterThanOrEquals';
import startsWith from './startsWith';
import isNegative from './isNegative';

const rules = {
endsWith,
Expand Down Expand Up @@ -54,7 +55,8 @@ const rules = {
numberEquals,
shorterThan,
shorterThanOrEquals,
startsWith
startsWith,
isNegative,
};

export default extendRules(rules);
10 changes: 10 additions & 0 deletions packages/n4s/src/rules/isNegative/index.js
@@ -0,0 +1,10 @@
import isNumeric from '../isNumeric';

function isNegative(value, arg1) {
ganeshpatil0101 marked this conversation as resolved.
Show resolved Hide resolved
if (isNumeric(value)) {
return Number(value) < 0;
}
return false;
}
isNegative.negativeForm = 'isPositive';
export default isNegative;
45 changes: 45 additions & 0 deletions packages/n4s/src/rules/isNegative/spec.js
@@ -0,0 +1,45 @@
import isNegative from '.';

describe('Tests isNegative rule', () => {
describe('First argument is negative', () => {
describe('When first argument negative number', () => {
it('Should return true for negative numer', () => {
expect(isNegative(-1)).toBe(true);
});
it('should return true for negative desimal number', () => {
expect(isNegative(-1.1)).toBe(true);
});
it('should return true for negative string number', () => {
expect(isNegative('-1')).toBe(true);
});
it('should return true for negative decimal string number', () => {
expect(isNegative('-1.10')).toBe(true);
});
});
describe('When argument positive number', () => {
it('should return false for positive number', () => {
expect(isNegative(10)).toBe(false);
});
it('should return false for positive desimal number', () => {
expect(isNegative(10.1)).toBe(false);
});
it('should return false for positive string number', () => {
expect(isNegative('10')).toBe(false);
});
});

describe('When argument undefined or null or string', () => {
it('should return false for undefined value', () => {
expect(isNegative()).toBe(false);
});
it('should return false for null value', () => {
expect(isNegative(null)).toBe(false);
});
});
describe('Expose isPositive ', () => {
it('Should expose negativeForm property', () => {
expect(isNegative.negativeForm).toBe('isPositive');
});
});
});
});
6 changes: 4 additions & 2 deletions packages/n4s/src/rules/spec.js
Expand Up @@ -28,7 +28,8 @@ const negativeRules = [
'isNotNull',
'doesNotEndWith',
'doesNotStartWith',
'isNotBetween'
'isNotBetween',
'isNegative',
];

const positiveRules = [
Expand Down Expand Up @@ -63,7 +64,8 @@ const positiveRules = [
'isNull',
'endsWith',
'startsWith',
'isBetween'
'isBetween',
'isPositive',
];

const allRules = [...new Set([...positiveRules, ...negativeRules])];
34 changes: 34 additions & 0 deletions packages/vest/docs/enforce.md
Expand Up @@ -68,6 +68,8 @@ Enforce rules are functions that allow you to test your data against different c
- [doesNotEndWith](#doesnotendwith)
- [startsWith](#startsWith)
- [doesNotStartWith](#doesnotstartwith)
- [isNegative](#isnegative)
- [isPositive](#ispositive)

### equals

Expand Down Expand Up @@ -1095,6 +1097,38 @@ enforce.extend({
enforce(user.email).isValidEmail();
```

## isNegative

### Description

Determine where number is negative

### Usage examples:

```js
enforce(-10).isNegative(); //true
enforce(-10.12).isNegative(); //true
enforce('-10.12').isNegative(); //true
enforce(10).isNegative(); //false
enforce('10').isNegative(); //false
```

## isPositive

### Description

Determine where number is positive

### Usage examples:

```js
enforce(10).isPositive(); //true
enforce(10.12).isPositive(); //true
enforce('10.12').isPositive(); //true
enforce(-10).isPositive(); //false
enforce('-10.12').isPositive(); //false
```

## Custom rules return value

Rules can either return boolean indicating success or failure, or an object with two keys. `pass` indicates whether the validation is successful or not, and message provides a function with no arguments that return an error message in case of failure. Thus, when pass is false, message should return the error message for when enforce(x).yourRule() fails.
Expand Down
12 changes: 10 additions & 2 deletions packages/vest/src/typings/vest.d.ts
Expand Up @@ -135,7 +135,10 @@ export interface IEnforceRules<T = {}> {
gte: (expected: number | string) => IEnforceRules<T> & EnforceExtendMap<T>;
lt: (expected: number | string) => IEnforceRules<T> & EnforceExtendMap<T>;
lte: (expected: number | string) => IEnforceRules<T> & EnforceExtendMap<T>;
isBetween: (start: number, end: number) => IEnforceRules<T> & EnforceExtendMap<T>;
isBetween: (
start: number,
end: number
) => IEnforceRules<T> & EnforceExtendMap<T>;
endsWith: (suffix: string) => IEnforceRules<T> & EnforceExtendMap<T>;
startsWith: (prefix: string) => IEnforceRules<T> & EnforceExtendMap<T>;
doesNotEndWith: (suffix: string) => IEnforceRules<T> & EnforceExtendMap<T>;
Expand Down Expand Up @@ -165,7 +168,10 @@ export interface IEnforceRules<T = {}> {
isNotEmpty: () => IEnforceRules<T> & EnforceExtendMap<T>;
isNotNumber: () => IEnforceRules<T> & EnforceExtendMap<T>;
isNotNumeric: () => IEnforceRules<T> & EnforceExtendMap<T>;
isNotBetween: (start: number, end: number) => IEnforceRules<T> & EnforceExtendMap<T>;
isNotBetween: (
start: number,
end: number
) => IEnforceRules<T> & EnforceExtendMap<T>;
isNotString: () => IEnforceRules<T> & EnforceExtendMap<T>;
inside: (
expected: Array<string | number | boolean> | string
Expand All @@ -175,6 +181,8 @@ export interface IEnforceRules<T = {}> {
) => IEnforceRules<T> & EnforceExtendMap<T>;
lengthEquals: (expected: number) => IEnforceRules<T> & EnforceExtendMap<T>;
lengthNotEquals: (expected: number) => IEnforceRules<T> & EnforceExtendMap<T>;
isNegative: (expected: number) => IEnforceRules<T> & EnforceExtendMap<T>;
isPositive: (expected: number) => IEnforceRules<T> & EnforceExtendMap<T>;
}

interface IEnforce {
Expand Down