Skip to content

Commit

Permalink
feature(#285): Add maximum/minimum Validators to Type.FLOAT & Type.IN…
Browse files Browse the repository at this point in the history
…TEGER
  • Loading branch information
FlorianWendelborn committed Sep 13, 2020
1 parent 555b285 commit 35cba51
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 8 deletions.
18 changes: 18 additions & 0 deletions packages/kotti-ui/source/kotti-col/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,51 @@ export const KtCol = makeInstallable(
props: vuePropsValidators.create({
lg: {
default: () => null,
maximum: 24,
minimum: 1,
nullable: true,
type: vuePropsValidators.Type.INTEGER,
},
md: {
default: () => null,
maximum: 24,
minimum: 1,
nullable: true,
type: vuePropsValidators.Type.INTEGER,
},
offset: {
default: () => null,
maximum: 24,
minimum: 1,
nullable: true,
type: vuePropsValidators.Type.INTEGER,
},
pull: {
default: () => null,
maximum: 24,
minimum: 1,
nullable: true,
type: vuePropsValidators.Type.INTEGER,
},
push: {
default: () => null,
maximum: 24,
minimum: 1,
nullable: true,
type: vuePropsValidators.Type.INTEGER,
},
sm: {
default: () => null,
maximum: 24,
minimum: 1,
nullable: true,
type: vuePropsValidators.Type.INTEGER,
},
span: {
// eslint-disable-next-line no-magic-numbers
default: () => 24,
maximum: 24,
minimum: 1,
nullable: false,
type: vuePropsValidators.Type.INTEGER,
},
Expand All @@ -61,11 +75,15 @@ export const KtCol = makeInstallable(
},
xl: {
default: () => null,
maximum: 24,
minimum: 1,
nullable: false,
type: vuePropsValidators.Type.INTEGER,
},
xs: {
default: () => null,
maximum: 24,
minimum: 1,
nullable: false,
type: vuePropsValidators.Type.INTEGER,
},
Expand Down
28 changes: 26 additions & 2 deletions packages/vue-props-validators/source/modules/float.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-magic-numbers */
import { vuePropsValidators } from '..'
import { REQUIRED } from '../constants'

Expand All @@ -23,12 +24,35 @@ test('float validator works', () => {

expect(example.validator('test')).toBeFalsy()
expect(example.validator(undefined)).toBeFalsy()
/* eslint-disable no-magic-numbers */
expect(example.validator(42)).toBeTruthy()
expect(example.validator(420)).toBeTruthy()
expect(example.validator(2.3)).toBeTruthy()
expect(example.validator(NaN)).toBeFalsy()
/* eslint-enable no-magic-numbers */
})

test('float (maximum)', () => {
const { example } = vuePropsValidators.create({
example: { ...BASE_FLOAT, maximum: 5.2 },
})

expect(example.validator(1)).toBeTruthy()
expect(example.validator(5)).toBeTruthy()
expect(example.validator(5.2)).toBeTruthy()
expect(example.validator(5.21)).toBeFalsy()
expect(example.validator(420)).toBeFalsy()
})

test('float (minimum)', () => {
const { example } = vuePropsValidators.create({
example: { ...BASE_FLOAT, minimum: 5.2 },
})

expect(example.validator(1)).toBeFalsy()
expect(example.validator(4)).toBeFalsy()
expect(example.validator(5)).toBeFalsy()
expect(example.validator(5.19)).toBeFalsy()
expect(example.validator(5.2)).toBeTruthy()
expect(example.validator(420)).toBeTruthy()
})

test('float (nullable: false)', () =>
Expand Down
10 changes: 9 additions & 1 deletion packages/vue-props-validators/source/modules/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { isNumber } from '../utilities'
import { TypeBase, Type } from '.'

export type TypeFloat = TypeBase & {
maximum?: number
minimum?: number
type: Type.FLOAT
}

Expand All @@ -14,5 +16,11 @@ export const createFloat = <OPTION extends TypeFloat>(
): Result<OPTION, NumberConstructor> => ({
...resolveDefault(option),
type: Number,
validator: baseValidator(option, (value: unknown) => isNumber(value)),
validator: baseValidator(
option,
(value: unknown) =>
isNumber(value) &&
(option.maximum === undefined || value <= option.maximum) &&
(option.minimum === undefined || value >= option.minimum),
),
})
25 changes: 23 additions & 2 deletions packages/vue-props-validators/source/modules/integer.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-magic-numbers */
import { vuePropsValidators } from '..'
import { REQUIRED } from '../constants'

Expand All @@ -23,12 +24,32 @@ test('integer validator works', () => {

expect(example.validator('test')).toBeFalsy()
expect(example.validator(undefined)).toBeFalsy()
/* eslint-disable no-magic-numbers */
expect(example.validator(42)).toBeTruthy()
expect(example.validator(420)).toBeTruthy()
expect(example.validator(2.3)).toBeFalsy()
expect(example.validator(NaN)).toBeFalsy()
/* eslint-enable no-magic-numbers */
})

test('integer (maximum)', () => {
const { example } = vuePropsValidators.create({
example: { ...BASE_INTEGER, maximum: 5 },
})

expect(example.validator(1)).toBeTruthy()
expect(example.validator(5)).toBeTruthy()
expect(example.validator(6)).toBeFalsy()
expect(example.validator(420)).toBeFalsy()
})

test('integer (minimum)', () => {
const { example } = vuePropsValidators.create({
example: { ...BASE_INTEGER, minimum: 5 },
})

expect(example.validator(1)).toBeFalsy()
expect(example.validator(4)).toBeFalsy()
expect(example.validator(5)).toBeTruthy()
expect(example.validator(420)).toBeTruthy()
})

test('integer (nullable: false)', () =>
Expand Down
8 changes: 7 additions & 1 deletion packages/vue-props-validators/source/modules/integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { isNumber } from '../utilities'
import { TypeBase, Type } from '.'

export type TypeInteger = TypeBase & {
maximum?: number
minimum?: number
type: Type.INTEGER
}

Expand All @@ -16,6 +18,10 @@ export const createInteger = <OPTION extends TypeInteger>(
type: Number,
validator: baseValidator(
option,
(value: unknown) => isNumber(value) && Number.isSafeInteger(value),
(value: unknown) =>
isNumber(value) &&
Number.isSafeInteger(value) &&
(option.maximum === undefined || value <= option.maximum) &&
(option.minimum === undefined || value >= option.minimum),
),
})
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const BASE_STRING: TypeString = {

test('string has correct type', () =>
expect(vuePropsValidators.create({ example: BASE_STRING })).toMatchObject({
example: { type: Number },
example: { type: String },
}))

test('string validator works', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/vue-props-validators/source/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
* We don't need the full package, as we don’t consider strings to be valid numbers
* @see {@link https://github.com/jonschlinkert/is-number/blob/98e8ff1da1a89f93d1397a24d7413ed15421c139/index.js#L11-L13}
*/
export const isNumber = (value: unknown): boolean =>
export const isNumber = (value: unknown): value is number =>
// eslint-disable-next-line sonarjs/no-identical-expressions
typeof value === 'number' ? value - value === 0 : false

0 comments on commit 35cba51

Please sign in to comment.