Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
195 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Big from 'big.js'; | ||
import { Keyword, ValidationCtxHighOrder } from '../interface/index'; | ||
export default { | ||
isHighOrder: true, | ||
name: 'max', | ||
validator(value, { path, state, args }: ValidationCtxHighOrder, cb) { | ||
const [max, isExclusive] = args; | ||
const valueBig = new Big(value); | ||
const maxBig = new Big(max); | ||
if (valueBig.gt(maxBig) || (isExclusive && valueBig.eq(maxBig))) { | ||
return cb({ | ||
KEY: isExclusive ? 'max_exclusive' : 'max', | ||
value, | ||
max, | ||
isExclusive | ||
}); | ||
} | ||
cb(); | ||
} | ||
} as Keyword; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Big from 'big.js'; | ||
import { Keyword, ValidationCtxHighOrder } from '../interface/index'; | ||
export default { | ||
isHighOrder: true, | ||
name: 'min', | ||
validator(value, { path, state, args }: ValidationCtxHighOrder, cb) { | ||
const [min, isExclusive] = args; | ||
const valueBig = new Big(value); | ||
const minBig = new Big(min); | ||
if (valueBig.lt(minBig) || (isExclusive && valueBig.eq(minBig))) { | ||
return cb({ | ||
KEY: isExclusive ? 'min_exclusive' : 'min', | ||
value, | ||
min, | ||
isExclusive | ||
}); | ||
} | ||
cb(); | ||
} | ||
} as Keyword; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
import Big from 'big.js'; | ||
import { Keyword } from '../interface/index'; | ||
const NUMERIC = /^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i; | ||
export default { | ||
name: 'number', | ||
validator(value, { path, state }, cb) { | ||
state.types[path] = 'number'; | ||
if (typeof value === 'number' || NUMERIC.test(value)) { | ||
return cb(); | ||
try { | ||
// tslint:disable no-unused-expression | ||
new Big(value); | ||
} catch (e) { | ||
return cb(true); | ||
} | ||
cb(true); | ||
cb(); | ||
} | ||
} as Keyword; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { runTest, TestCaseConfig } from '../runTest'; | ||
|
||
import _ from '../../src'; | ||
|
||
const cases: TestCaseConfig = { | ||
name: 'max', | ||
cases: [ | ||
{ | ||
schema: _.number.max(2), | ||
pass: [-1, '-1', 0, 1, '1', '1.1', 2, '2'], | ||
fail: [2.2, '2.3', 3, '3', 100, '100'] | ||
}, | ||
{ | ||
schema: _.number.max(2, true), | ||
pass: [-1, '-1', 0, 1, '1', '1.1'], | ||
fail: [2, '2', 2.2, '2.3', 3, '3', 100, '100'] | ||
} | ||
] | ||
}; | ||
runTest(cases); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { runTest, TestCaseConfig } from '../runTest'; | ||
|
||
import _ from '../../src'; | ||
|
||
const cases: TestCaseConfig = { | ||
name: 'min', | ||
cases: [ | ||
{ | ||
schema: _.number.min(1), | ||
pass: [1, '1', '1.1', 1.1e2], | ||
fail: ['0', '0.1', 0.5] | ||
}, | ||
{ | ||
schema: _.number.min(1, true), | ||
pass: [1.1, '1.1', 1.1e2], | ||
fail: ['0', '0.1', 0.5, 1, '1'] | ||
} | ||
] | ||
}; | ||
runTest(cases); |