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

Add validateMaximum & validateMinimum #3703

Merged
merged 5 commits into from
Sep 29, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions src/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,18 @@ export const propChecker = (props, nextProps, objectList=[], ignoreList=[]) => {
|| objectList.some( objectPropName => !eq(props[objectPropName], nextProps[objectPropName])))
}

export const validateMaximum = ( val, max ) => {
if (val > max) {
return "Value must be less than Maximum"
}
}

export const validateMinimum = ( val, min ) => {
if (val < min) {
return "Value must be greater than Minimum"
}
}

export const validateNumber = ( val ) => {
if (!/^-?\d+(\.?\d+)?$/.test(val)) {
return "Value must be a number"
Expand Down Expand Up @@ -517,6 +529,8 @@ export const validateParam = (param, isXml) => {
let errors = []
let value = isXml && param.get("in") === "body" ? param.get("value_xml") : param.get("value")
let required = param.get("required")
let maximum = param.get("maximum")
let minimum = param.get("minimum")
let type = param.get("type")
let format = param.get("format")

Expand All @@ -539,6 +553,16 @@ export const validateParam = (param, isXml) => {
errors.push("Required field is not provided")
return errors
}

if (maximum) {
let err = validateMaximum(value, maximum)
if (err) errors.push(err)
}

if (minimum) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this handle negative minimum values?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure it will, what makes you think it won't?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought for a second that negative integers would evaluate as false, but I was wrong. The only number that does is 0.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding the test to verify!

let err = validateMinimum(value, minimum)
if (err) errors.push(err)
}

if ( type === "string" ) {
let err
Expand Down
35 changes: 33 additions & 2 deletions test/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import expect from "expect"
import { fromJS, OrderedMap } from "immutable"
import { mapToList, validateDateTime, validateGuid, validateNumber, validateInteger, validateParam, validateFile, fromJSOrdered, getAcceptControllingResponse, createDeepLinkPath, escapeDeepLinkPath } from "core/utils"
import { validateMaximum, validateMinimum } from "core/utils"
import win from "core/window"

describe("utils", function() {
Expand Down Expand Up @@ -72,6 +73,34 @@ describe("utils", function() {

})

describe("validateMaximum", function() {
let errorMessage = "Value must be less than Maximum"

it("doesn't return for valid input", function() {
expect(validateMaximum(9, 10)).toBeFalsy()
expect(validateMaximum(19, 20)).toBeFalsy()
})

it("returns a message for invalid input", function() {
expect(validateMaximum(10, 9)).toEqual(errorMessage)
expect(validateMaximum(20, 19)).toEqual(errorMessage)
})
})

describe("validateMinimum", function() {
let errorMessage = "Value must be greater than Minimum"

it("doesn't return for valid input", function() {
expect(validateMinimum(2, 1)).toBeFalsy()
expect(validateMinimum(20, 10)).toBeFalsy()
})

it("returns a message for invalid input", function() {
expect(validateMinimum(1, 2)).toEqual(errorMessage)
expect(validateMinimum(10, 20)).toEqual(errorMessage)
})
})

describe("validateNumber", function() {
let errorMessage = "Value must be a number"

Expand Down Expand Up @@ -485,11 +514,13 @@ describe("utils", function() {
result = validateParam( param, false )
expect( result ).toEqual( ["Required field is not provided"] )

// valid number
// valid number with min and max
param = fromJS({
required: true,
type: "number",
value: 10
value: 10,
minimum: 5,
maximum: 99
})
result = validateParam( param, false )
expect( result ).toEqual( [] )
Expand Down