Revert "Merge pull request #3427 from owenconti/bug/3414-validate-params-reworked"
Reopens #3414 This reverts commit99d368673b, reversing changes made to8bf9037bbc.
This commit is contained in:
@@ -129,8 +129,7 @@ export class Select extends React.Component {
|
|||||||
value: PropTypes.any,
|
value: PropTypes.any,
|
||||||
onChange: PropTypes.func,
|
onChange: PropTypes.func,
|
||||||
multiple: PropTypes.bool,
|
multiple: PropTypes.bool,
|
||||||
allowEmptyValue: PropTypes.bool,
|
allowEmptyValue: PropTypes.bool
|
||||||
className: PropTypes.string
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static defaultProps = {
|
static defaultProps = {
|
||||||
@@ -143,7 +142,7 @@ export class Select extends React.Component {
|
|||||||
|
|
||||||
let value
|
let value
|
||||||
|
|
||||||
if (props.value) {
|
if (props.value !== undefined) {
|
||||||
value = props.value
|
value = props.value
|
||||||
} else {
|
} else {
|
||||||
value = props.multiple ? [""] : ""
|
value = props.multiple ? [""] : ""
|
||||||
@@ -179,7 +178,7 @@ export class Select extends React.Component {
|
|||||||
let value = this.state.value.toJS ? this.state.value.toJS() : this.state.value
|
let value = this.state.value.toJS ? this.state.value.toJS() : this.state.value
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<select className={this.props.className} multiple={ multiple } value={ value } onChange={ this.onChange } >
|
<select multiple={ multiple } value={ value } onChange={ this.onChange } >
|
||||||
{ allowEmptyValue ? <option value="">--</option> : null }
|
{ allowEmptyValue ? <option value="">--</option> : null }
|
||||||
{
|
{
|
||||||
allowedValues.map(function (item, key) {
|
allowedValues.map(function (item, key) {
|
||||||
|
|||||||
@@ -57,8 +57,7 @@ export class JsonSchema_string extends Component {
|
|||||||
|
|
||||||
if ( enumValue ) {
|
if ( enumValue ) {
|
||||||
const Select = getComponent("Select")
|
const Select = getComponent("Select")
|
||||||
return (<Select className={ errors.length ? "invalid" : ""}
|
return (<Select allowedValues={ enumValue }
|
||||||
allowedValues={ enumValue }
|
|
||||||
value={ value }
|
value={ value }
|
||||||
allowEmptyValue={ !required }
|
allowEmptyValue={ !required }
|
||||||
onChange={ this.onEnumChange }/>)
|
onChange={ this.onEnumChange }/>)
|
||||||
@@ -122,7 +121,6 @@ export class JsonSchema_array extends PureComponent {
|
|||||||
render() {
|
render() {
|
||||||
let { getComponent, required, schema, fn } = this.props
|
let { getComponent, required, schema, fn } = this.props
|
||||||
|
|
||||||
let errors = schema.errors || []
|
|
||||||
let itemSchema = fn.inferSchema(schema.items)
|
let itemSchema = fn.inferSchema(schema.items)
|
||||||
|
|
||||||
const JsonSchemaForm = getComponent("JsonSchemaForm")
|
const JsonSchemaForm = getComponent("JsonSchemaForm")
|
||||||
@@ -133,17 +131,19 @@ export class JsonSchema_array extends PureComponent {
|
|||||||
|
|
||||||
if ( enumValue ) {
|
if ( enumValue ) {
|
||||||
const Select = getComponent("Select")
|
const Select = getComponent("Select")
|
||||||
return (<Select className={ errors.length ? "invalid" : ""}
|
return (<Select multiple={ true }
|
||||||
multiple={ true }
|
|
||||||
value={ value }
|
value={ value }
|
||||||
allowedValues={ enumValue }
|
allowedValues={ enumValue }
|
||||||
allowEmptyValue={ !required }
|
allowEmptyValue={ !required }
|
||||||
onChange={ this.onEnumChange }/>)
|
onChange={ this.onEnumChange }/>)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let errors = schema.errors || []
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
{ !value || value.count() < 1 ? null :
|
{ !value || value.count() < 1 ?
|
||||||
|
(errors.length ? <span style={{ color: "red", fortWeight: "bold" }}>{ errors[0] }</span> : null) :
|
||||||
value.map( (item,i) => {
|
value.map( (item,i) => {
|
||||||
let schema = Object.assign({}, itemSchema)
|
let schema = Object.assign({}, itemSchema)
|
||||||
if ( errors.length ) {
|
if ( errors.length ) {
|
||||||
@@ -153,12 +153,12 @@ export class JsonSchema_array extends PureComponent {
|
|||||||
return (
|
return (
|
||||||
<div key={i} className="json-schema-form-item">
|
<div key={i} className="json-schema-form-item">
|
||||||
<JsonSchemaForm fn={fn} getComponent={getComponent} value={item} onChange={(val) => this.onItemChange(val, i)} schema={schema} />
|
<JsonSchemaForm fn={fn} getComponent={getComponent} value={item} onChange={(val) => this.onItemChange(val, i)} schema={schema} />
|
||||||
<Button className="btn btn-sm json-schema-form-item-remove" onClick={()=> this.removeItem(i)} > - </Button>
|
<Button className="json-schema-form-item-remove" onClick={()=> this.removeItem(i)} > - </Button>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}).toArray()
|
}).toArray()
|
||||||
}
|
}
|
||||||
<Button className={`btn btn-sm json-schema-form-item-add ${errors.length ? "invalid" : null}`} onClick={this.addItem}> Add item </Button>
|
<Button className="json-schema-form-item-add" onClick={this.addItem}> Add item </Button>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -170,14 +170,12 @@ export class JsonSchema_boolean extends Component {
|
|||||||
|
|
||||||
onEnumChange = (val) => this.props.onChange(val)
|
onEnumChange = (val) => this.props.onChange(val)
|
||||||
render() {
|
render() {
|
||||||
let { getComponent, value, schema } = this.props
|
let { getComponent, required, value } = this.props
|
||||||
let errors = schema.errors || []
|
|
||||||
const Select = getComponent("Select")
|
const Select = getComponent("Select")
|
||||||
|
|
||||||
return (<Select className={ errors.length ? "invalid" : ""}
|
return (<Select value={ String(value) }
|
||||||
value={ String(value) }
|
|
||||||
allowedValues={ fromJS(["true", "false"]) }
|
allowedValues={ fromJS(["true", "false"]) }
|
||||||
allowEmptyValue={true}
|
allowEmptyValue={ !required }
|
||||||
onChange={ this.onEnumChange }/>)
|
onChange={ this.onEnumChange }/>)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -468,18 +468,6 @@ export const validateFile = ( val ) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const validateBoolean = ( val ) => {
|
|
||||||
if ( !(val === "true" || val === "false" || val === true || val === false) ) {
|
|
||||||
return "Value must be a boolean"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const validateString = ( val ) => {
|
|
||||||
if ( val && typeof val !== "string" ) {
|
|
||||||
return "Value must be a string"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// validation of parameters before execute
|
// validation of parameters before execute
|
||||||
export const validateParam = (param, isXml) => {
|
export const validateParam = (param, isXml) => {
|
||||||
let errors = []
|
let errors = []
|
||||||
@@ -487,32 +475,21 @@ export const validateParam = (param, isXml) => {
|
|||||||
let required = param.get("required")
|
let required = param.get("required")
|
||||||
let type = param.get("type")
|
let type = param.get("type")
|
||||||
|
|
||||||
// If the parameter is required OR the parameter has a value (meaning optional, but filled in)
|
let stringCheck = type === "string" && !value
|
||||||
// then we should do our validation routine
|
let arrayCheck = type === "array" && Array.isArray(value) && !value.length
|
||||||
if ( required || value ) {
|
let listCheck = type === "array" && Im.List.isList(value) && !value.count()
|
||||||
// These checks should evaluate to true if the parameter's value is valid
|
let fileCheck = type === "file" && !(value instanceof win.File)
|
||||||
let stringCheck = type === "string" && value && !validateString(value)
|
|
||||||
let arrayCheck = type === "array" && Array.isArray(value) && value.length
|
|
||||||
let listCheck = type === "array" && Im.List.isList(value) && value.count()
|
|
||||||
let fileCheck = type === "file" && value instanceof win.File
|
|
||||||
let booleanCheck = type === "boolean" && !validateBoolean(value)
|
|
||||||
let numberCheck = type === "number" && !validateNumber(value) // validateNumber returns undefined if the value is a number
|
|
||||||
let integerCheck = type === "integer" && !validateInteger(value) // validateInteger returns undefined if the value is an integer
|
|
||||||
|
|
||||||
if ( required && !(stringCheck || arrayCheck || listCheck || fileCheck || booleanCheck || numberCheck || integerCheck) ) {
|
if ( required && (stringCheck || arrayCheck || listCheck || fileCheck) ) {
|
||||||
errors.push("Required field is not provided")
|
errors.push("Required field is not provided")
|
||||||
return errors
|
return errors
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( type === "string" ) {
|
if ( value === null || value === undefined ) {
|
||||||
let err = validateString(value)
|
return errors
|
||||||
if (!err) return errors
|
}
|
||||||
errors.push(err)
|
|
||||||
} else if ( type === "boolean" ) {
|
if ( type === "number" ) {
|
||||||
let err = validateBoolean(value)
|
|
||||||
if (!err) return errors
|
|
||||||
errors.push(err)
|
|
||||||
} else if ( type === "number" ) {
|
|
||||||
let err = validateNumber(value)
|
let err = validateNumber(value)
|
||||||
if (!err) return errors
|
if (!err) return errors
|
||||||
errors.push(err)
|
errors.push(err)
|
||||||
@@ -534,8 +511,6 @@ export const validateParam = (param, isXml) => {
|
|||||||
err = validateNumber(item)
|
err = validateNumber(item)
|
||||||
} else if (itemType === "integer") {
|
} else if (itemType === "integer") {
|
||||||
err = validateInteger(item)
|
err = validateInteger(item)
|
||||||
} else if (itemType === "string") {
|
|
||||||
err = validateString(item)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( err ) {
|
if ( err ) {
|
||||||
@@ -547,7 +522,6 @@ export const validateParam = (param, isXml) => {
|
|||||||
if (!err) return errors
|
if (!err) return errors
|
||||||
errors.push(err)
|
errors.push(err)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return errors
|
return errors
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,11 +14,6 @@
|
|||||||
|
|
||||||
@include text_headline();
|
@include text_headline();
|
||||||
|
|
||||||
&.btn-sm {
|
|
||||||
font-size: 12px;
|
|
||||||
padding: 4px 23px;
|
|
||||||
}
|
|
||||||
|
|
||||||
&[disabled]
|
&[disabled]
|
||||||
{
|
{
|
||||||
cursor: not-allowed;
|
cursor: not-allowed;
|
||||||
@@ -170,9 +165,6 @@
|
|||||||
button
|
button
|
||||||
{
|
{
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
outline: none;
|
|
||||||
|
|
||||||
&.invalid {
|
outline: none;
|
||||||
@include invalidFormElement();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,10 +21,6 @@ select
|
|||||||
|
|
||||||
background: #f7f7f7;
|
background: #f7f7f7;
|
||||||
}
|
}
|
||||||
|
|
||||||
&.invalid {
|
|
||||||
@include invalidFormElement();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.opblock-body select
|
.opblock-body select
|
||||||
@@ -57,8 +53,12 @@ input[type=file]
|
|||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
background: #fff;
|
background: #fff;
|
||||||
|
|
||||||
&.invalid {
|
&.invalid
|
||||||
@include invalidFormElement();
|
{
|
||||||
|
animation: shake .4s 1;
|
||||||
|
|
||||||
|
border-color: $_color-delete;
|
||||||
|
background: lighten($_color-delete, 35%);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -166,9 +166,3 @@ $browser-context: 16;
|
|||||||
@warn 'Breakpoint mixin supports: tablet, mobile, desktop';
|
@warn 'Breakpoint mixin supports: tablet, mobile, desktop';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@mixin invalidFormElement() {
|
|
||||||
animation: shake .4s 1;
|
|
||||||
border-color: $_color-delete;
|
|
||||||
background: lighten($_color-delete, 35%);
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -97,10 +97,6 @@ table
|
|||||||
width: 100%;
|
width: 100%;
|
||||||
max-width: 340px;
|
max-width: 340px;
|
||||||
}
|
}
|
||||||
|
|
||||||
select {
|
|
||||||
border-width: 1px;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.parameter__name
|
.parameter__name
|
||||||
|
|||||||
@@ -176,7 +176,6 @@ describe("utils", function() {
|
|||||||
let result = null
|
let result = null
|
||||||
|
|
||||||
it("validates required strings", function() {
|
it("validates required strings", function() {
|
||||||
// invalid string
|
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: true,
|
required: true,
|
||||||
type: "string",
|
type: "string",
|
||||||
@@ -184,39 +183,9 @@ describe("utils", function() {
|
|||||||
})
|
})
|
||||||
result = validateParam( param, false )
|
result = validateParam( param, false )
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
expect( result ).toEqual( ["Required field is not provided"] )
|
||||||
|
|
||||||
// valid string
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "string",
|
|
||||||
value: "test string"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
|
||||||
|
|
||||||
it("validates optional strings", function() {
|
|
||||||
// valid (empty) string
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "string",
|
|
||||||
value: ""
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// valid string
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "string",
|
|
||||||
value: "test"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("validates required files", function() {
|
it("validates required files", function() {
|
||||||
// invalid file
|
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: true,
|
required: true,
|
||||||
type: "file",
|
type: "file",
|
||||||
@@ -224,48 +193,9 @@ describe("utils", function() {
|
|||||||
})
|
})
|
||||||
result = validateParam( param, false )
|
result = validateParam( param, false )
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
expect( result ).toEqual( ["Required field is not provided"] )
|
||||||
|
|
||||||
// valid file
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "file",
|
|
||||||
value: new win.File()
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
|
||||||
|
|
||||||
it("validates optional files", function() {
|
|
||||||
// invalid file
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "file",
|
|
||||||
value: "not a file"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Value must be a file"] )
|
|
||||||
|
|
||||||
// valid (empty) file
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "file",
|
|
||||||
value: undefined
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// valid file
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "file",
|
|
||||||
value: new win.File()
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("validates required arrays", function() {
|
it("validates required arrays", function() {
|
||||||
// invalid (empty) array
|
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: true,
|
required: true,
|
||||||
type: "array",
|
type: "array",
|
||||||
@@ -274,51 +204,75 @@ describe("utils", function() {
|
|||||||
result = validateParam( param, false )
|
result = validateParam( param, false )
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
expect( result ).toEqual( ["Required field is not provided"] )
|
||||||
|
|
||||||
// invalid (not an array)
|
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: true,
|
required: true,
|
||||||
type: "array",
|
type: "array",
|
||||||
value: undefined
|
value: []
|
||||||
})
|
})
|
||||||
result = validateParam( param, false )
|
result = validateParam( param, false )
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
expect( result ).toEqual( ["Required field is not provided"] )
|
||||||
|
})
|
||||||
|
|
||||||
// invalid array, items do not match correct type
|
it("validates numbers", function() {
|
||||||
|
// string instead of a number
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: true,
|
required: false,
|
||||||
type: "array",
|
type: "number",
|
||||||
value: [1],
|
value: "test"
|
||||||
items: {
|
|
||||||
type: "string"
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
result = validateParam( param, false )
|
result = validateParam( param, false )
|
||||||
expect( result ).toEqual( [{index: 0, error: "Value must be a string"}] )
|
expect( result ).toEqual( ["Value must be a number"] )
|
||||||
|
|
||||||
// valid array, with no 'type' for items
|
// undefined value
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: true,
|
required: false,
|
||||||
type: "array",
|
type: "number",
|
||||||
value: ["1"]
|
value: undefined
|
||||||
})
|
})
|
||||||
result = validateParam( param, false )
|
result = validateParam( param, false )
|
||||||
expect( result ).toEqual( [] )
|
expect( result ).toEqual( [] )
|
||||||
|
|
||||||
// valid array, items match type
|
// null value
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: true,
|
required: false,
|
||||||
type: "array",
|
type: "number",
|
||||||
value: ["1"],
|
value: null
|
||||||
items: {
|
|
||||||
type: "string"
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
result = validateParam( param, false )
|
result = validateParam( param, false )
|
||||||
expect( result ).toEqual( [] )
|
expect( result ).toEqual( [] )
|
||||||
})
|
})
|
||||||
|
|
||||||
it("validates optional arrays", function() {
|
it("validates integers", function() {
|
||||||
// valid, empty array
|
// string instead of integer
|
||||||
|
param = fromJS({
|
||||||
|
required: false,
|
||||||
|
type: "integer",
|
||||||
|
value: "test"
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( ["Value must be an integer"] )
|
||||||
|
|
||||||
|
// undefined value
|
||||||
|
param = fromJS({
|
||||||
|
required: false,
|
||||||
|
type: "integer",
|
||||||
|
value: undefined
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
|
||||||
|
// null value
|
||||||
|
param = fromJS({
|
||||||
|
required: false,
|
||||||
|
type: "integer",
|
||||||
|
value: null
|
||||||
|
})
|
||||||
|
result = validateParam( param, false )
|
||||||
|
expect( result ).toEqual( [] )
|
||||||
|
})
|
||||||
|
|
||||||
|
it("validates arrays", function() {
|
||||||
|
// empty array
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: false,
|
required: false,
|
||||||
type: "array",
|
type: "array",
|
||||||
@@ -327,7 +281,7 @@ describe("utils", function() {
|
|||||||
result = validateParam( param, false )
|
result = validateParam( param, false )
|
||||||
expect( result ).toEqual( [] )
|
expect( result ).toEqual( [] )
|
||||||
|
|
||||||
// invalid, items do not match correct type
|
// numbers
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: false,
|
required: false,
|
||||||
type: "array",
|
type: "array",
|
||||||
@@ -339,209 +293,17 @@ describe("utils", function() {
|
|||||||
result = validateParam( param, false )
|
result = validateParam( param, false )
|
||||||
expect( result ).toEqual( [{index: 0, error: "Value must be a number"}] )
|
expect( result ).toEqual( [{index: 0, error: "Value must be a number"}] )
|
||||||
|
|
||||||
// valid
|
// integers
|
||||||
param = fromJS({
|
param = fromJS({
|
||||||
required: false,
|
required: false,
|
||||||
type: "array",
|
type: "array",
|
||||||
value: ["test"],
|
value: ["not", "numbers"],
|
||||||
items: {
|
items: {
|
||||||
type: "string"
|
type: "integer"
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
result = validateParam( param, false )
|
result = validateParam( param, false )
|
||||||
expect( result ).toEqual( [] )
|
expect( result ).toEqual( [{index: 0, error: "Value must be an integer"}, {index: 1, error: "Value must be an integer"}] )
|
||||||
})
|
|
||||||
|
|
||||||
it("validates required booleans", function() {
|
|
||||||
// invalid boolean value
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "boolean",
|
|
||||||
value: undefined
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
|
||||||
|
|
||||||
// invalid boolean value (not a boolean)
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "boolean",
|
|
||||||
value: "test string"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
|
||||||
|
|
||||||
// valid boolean value
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "boolean",
|
|
||||||
value: "true"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// valid boolean value
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "boolean",
|
|
||||||
value: false
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
|
||||||
|
|
||||||
it("validates optional booleans", function() {
|
|
||||||
// valid (empty) boolean value
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "boolean",
|
|
||||||
value: undefined
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// invalid boolean value (not a boolean)
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "boolean",
|
|
||||||
value: "test string"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Value must be a boolean"] )
|
|
||||||
|
|
||||||
// valid boolean value
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "boolean",
|
|
||||||
value: "true"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// valid boolean value
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "boolean",
|
|
||||||
value: false
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
|
||||||
|
|
||||||
it("validates required numbers", function() {
|
|
||||||
// invalid number, string instead of a number
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "number",
|
|
||||||
value: "test"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
|
||||||
|
|
||||||
// invalid number, undefined value
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "number",
|
|
||||||
value: undefined
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
|
||||||
|
|
||||||
// valid number
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "number",
|
|
||||||
value: 10
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
|
||||||
|
|
||||||
it("validates optional numbers", function() {
|
|
||||||
// invalid number, string instead of a number
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "number",
|
|
||||||
value: "test"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Value must be a number"] )
|
|
||||||
|
|
||||||
// valid (empty) number
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "number",
|
|
||||||
value: undefined
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// valid number
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "number",
|
|
||||||
value: 10
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
|
||||||
|
|
||||||
it("validates required integers", function() {
|
|
||||||
// invalid integer, string instead of an integer
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "integer",
|
|
||||||
value: "test"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
|
||||||
|
|
||||||
// invalid integer, undefined value
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "integer",
|
|
||||||
value: undefined
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Required field is not provided"] )
|
|
||||||
|
|
||||||
// valid integer
|
|
||||||
param = fromJS({
|
|
||||||
required: true,
|
|
||||||
type: "integer",
|
|
||||||
value: 10
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
|
||||||
|
|
||||||
it("validates optional integers", function() {
|
|
||||||
// invalid integer, string instead of an integer
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "integer",
|
|
||||||
value: "test"
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( ["Value must be an integer"] )
|
|
||||||
|
|
||||||
// valid (empty) integer
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "integer",
|
|
||||||
value: undefined
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
|
|
||||||
// valid number
|
|
||||||
param = fromJS({
|
|
||||||
required: false,
|
|
||||||
type: "integer",
|
|
||||||
value: 10
|
|
||||||
})
|
|
||||||
result = validateParam( param, false )
|
|
||||||
expect( result ).toEqual( [] )
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user