Skip to content

Commit

Permalink
Merge pull request #63 from A11oW/master
Browse files Browse the repository at this point in the history
[added] number methods less, more, notEqual
  • Loading branch information
jquense authored Oct 13, 2017
2 parents 1ca2044 + e3c5c32 commit b9575ab
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/locale.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export let string = {
export let number = {
min: '${path} must be greater than or equal to ${min}',
max: '${path} must be less than or equal to ${max}',
less: '${path} must be less than ${less}',
more: '${path} must be greater than ${more}',
notEqual: '${path} must be not equal to ${notEqual}',
positive: '${path} must be a positive number',
negative: '${path} must be a negative number',
integer: '${path} must be an integer',
Expand Down
43 changes: 39 additions & 4 deletions src/number.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
import inherits from './util/inherits';
import MixedSchema from './mixed';
import { number as locale} from './locale.js';
import { number as locale } from './locale.js';
import isAbsent from './util/isAbsent';


let isNaN = value => value != +value

let isInteger = val => isAbsent(val) || val === (val | 0)

export default function NumberSchema() {
if ( !(this instanceof NumberSchema))
if (!(this instanceof NumberSchema))
return new NumberSchema()

MixedSchema.call(this, { type: 'number' })

this.withMutation(() => {
this.transform(function(value) {
this.transform(function (value) {
if (this.isType(value)) return value

let parsed = parseFloat(value);
Expand Down Expand Up @@ -59,6 +58,42 @@ inherits(NumberSchema, MixedSchema, {
})
},

less(less, msg) {
return this.test({
name: 'less',
exclusive: true,
params: { less },
message: msg || locale.less,
test(value) {
return isAbsent(value) || value < this.resolve(less)
}
})
},

more(more, msg) {
return this.test({
name: 'more',
exclusive: true,
params: { more },
message: msg || locale.more,
test(value) {
return isAbsent(value) || value > this.resolve(more)
}
})
},

notEqual(notEqual, msg) {
return this.test({
name: 'notEqual',
exclusive: true,
params: { notEqual },
message: msg || locale.notEqual,
test(value) {
return isAbsent(value) || value !== this.resolve(notEqual)
}
})
},

positive(msg) {
return this.min(0, msg || locale.positive)
},
Expand Down
53 changes: 53 additions & 0 deletions test/number.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,59 @@ describe('Number types', function() {
})
})

describe('less', () => {
var schema = number().less(5);

TestHelpers.validateAll(schema, {
valid: [
4,
-10,
[null, schema.nullable()]
],
invalid: [
5,
7,
null,
[14, schema.less(10).less(14)]
]
})
})

describe('more', () => {
var schema = number().more(5);

TestHelpers.validateAll(schema, {
valid: [
6,
56445435,
[null, schema.nullable()]
],
invalid: [
5,
-10,
null,
[64, schema.more(52).more(74)]
]
})
})

describe('notEqual', () => {
var schema = number().notEqual(5);

TestHelpers.validateAll(schema, {
valid: [
6,
56445435,
[null, schema.nullable()]
],
invalid: [
5,
null,
[52, schema.notEqual(52).notEqual(74)]
]
})
})

describe('integer', ()=> {
TestHelpers.validateAll(
number().integer(),
Expand Down

0 comments on commit b9575ab

Please sign in to comment.