2222 * SOFTWARE.
2323 */
2424
25- import SchemaUtils from "./utils" ;
25+ import utils from "./utils" ;
2626import { Schema } from "./schema" ;
2727import { SchemaError } from "./schema-error" ;
2828
@@ -55,7 +55,7 @@ export class SchemaField {
5555
5656 constructor ( fieldName , props ) {
5757 // Default properties
58- props = SchemaUtils . extend ( {
58+ props = utils . extend ( {
5959 allowed : undefined ,
6060 check : undefined ,
6161 clean : undefined ,
@@ -84,7 +84,7 @@ export class SchemaField {
8484 // Check field properties
8585 for ( let prop in props ) {
8686 if ( props . hasOwnProperty ( prop ) ) {
87- if ( ! SchemaUtils . contains ( fieldProperties , prop ) ) {
87+ if ( ! utils . contains ( fieldProperties , prop ) ) {
8888 console . warn ( `Unknown schema field property "${ fieldName } .${ prop } "` ) ;
8989 }
9090 // Assign property
@@ -93,19 +93,18 @@ export class SchemaField {
9393 }
9494
9595 // Check field type
96- if ( typeof props . type === "undefined" || props . type === null ) {
97- throw new TypeError ( `${ fieldName } .type is missing` ) ;
98- }
99- else if ( props . type instanceof Array ) {
100- const arrayType = props . type [ 0 ] ;
96+ if ( typeof props . type !== "undefined" && props . type !== null ) {
97+ if ( props . type instanceof Array ) {
98+ const arrayType = props . type [ 0 ] ;
10199
102- // Check that array type is a function or class
103- if ( typeof arrayType !== "function" && typeof arrayType !== "object" ) {
104- throw new TypeError ( `${ fieldName } .type[] must contain a class or a function` ) ;
100+ // Check that array type is a function or class
101+ if ( typeof arrayType !== "function" && typeof arrayType !== "object" ) {
102+ throw new TypeError ( `${ fieldName } .type[] must contain a class or a function` ) ;
103+ }
104+ }
105+ else if ( ! utils . contains ( [ "function" , "object" ] , typeof props . type ) ) {
106+ throw new TypeError ( `${ fieldName } .type = "${ props . type } " is not a valid type` ) ;
105107 }
106- }
107- else if ( ! SchemaUtils . contains ( [ "function" , "object" ] , typeof props . type ) ) {
108- throw new TypeError ( `${ fieldName } .type = "${ props . type } " is not a valid type` ) ;
109108 }
110109
111110 // Check allowed values
@@ -124,7 +123,7 @@ export class SchemaField {
124123 }
125124
126125 // Check number decimal
127- if ( typeof props . decimal !== "undefined" && ! SchemaUtils . contains ( [ "function" , "boolean" ] , typeof props . decimal ) ) {
126+ if ( typeof props . decimal !== "undefined" && ! utils . contains ( [ "function" , "boolean" ] , typeof props . decimal ) ) {
128127 throw new TypeError ( `${ fieldName } .decimal must be a boolean or function` ) ;
129128 }
130129
@@ -134,7 +133,7 @@ export class SchemaField {
134133 }
135134
136135 // Set default label if missing
137- if ( typeof props . label !== "undefined" && ! SchemaUtils . contains ( [ "function" , "string" ] , typeof props . label ) ) {
136+ if ( typeof props . label !== "undefined" && ! utils . contains ( [ "function" , "string" ] , typeof props . label ) ) {
138137 throw new TypeError ( `${ fieldName } .label must be a string or function` ) ;
139138 }
140139
@@ -144,33 +143,33 @@ export class SchemaField {
144143 if ( props . length . length > 2 ) {
145144 throw new RangeError ( `${ fieldName } .length must only have 2 values [min, max]` ) ;
146145 }
147- } else if ( ! SchemaUtils . contains ( [ "function" , "number" ] , typeof props . length ) ) {
146+ } else if ( ! utils . contains ( [ "function" , "number" ] , typeof props . length ) ) {
148147 throw new TypeError ( `${ fieldName } .length must be a function, a number or an array[min, max]` ) ;
149148 }
150149 }
151150
152151 // Check max value
153- if ( typeof props . max !== "undefined" && ! SchemaUtils . contains ( [ "function" , "number" , "string" ] , typeof props . max ) && ! ( props . max instanceof Date ) ) {
152+ if ( typeof props . max !== "undefined" && ! utils . contains ( [ "function" , "number" , "string" ] , typeof props . max ) && ! ( props . max instanceof Date ) ) {
154153 throw new TypeError ( `${ fieldName } .max must be a date, number, string or function` ) ;
155154 }
156155
157156 // Check max words
158- if ( typeof props . maxWords !== "undefined" && ! SchemaUtils . contains ( [ "function" , "number" ] , typeof props . maxWords ) ) {
157+ if ( typeof props . maxWords !== "undefined" && ! utils . contains ( [ "function" , "number" ] , typeof props . maxWords ) ) {
159158 throw new TypeError ( `${ fieldName } .maxWords must be a number or function` ) ;
160159 }
161160
162161 // Check min value
163- if ( typeof props . min !== "undefined" && ! SchemaUtils . contains ( [ "function" , "number" , "string" ] , typeof props . min ) && ! ( props . min instanceof Date ) ) {
162+ if ( typeof props . min !== "undefined" && ! utils . contains ( [ "function" , "number" , "string" ] , typeof props . min ) && ! ( props . min instanceof Date ) ) {
164163 throw new TypeError ( `${ fieldName } .min must be a date, number, string or function` ) ;
165164 }
166165
167166 // Check min words
168- if ( typeof props . minWords !== "undefined" && ! SchemaUtils . contains ( [ "function" , "number" ] , typeof props . minWords ) ) {
167+ if ( typeof props . minWords !== "undefined" && ! utils . contains ( [ "function" , "number" ] , typeof props . minWords ) ) {
169168 throw new TypeError ( `${ fieldName } .minWords must be a number or function` ) ;
170169 }
171170
172171 // Check if field is nullable
173- if ( typeof props . nullable !== "undefined" && ! SchemaUtils . contains ( [ "function" , "boolean" ] , typeof props . nullable ) ) {
172+ if ( typeof props . nullable !== "undefined" && ! utils . contains ( [ "function" , "boolean" ] , typeof props . nullable ) ) {
174173 throw new TypeError ( `${ fieldName } .nullable must be a boolean or function` ) ;
175174 }
176175
@@ -180,12 +179,12 @@ export class SchemaField {
180179 }
181180
182181 // Check regular expression
183- if ( typeof props . regEx !== "undefined" && ! SchemaUtils . contains ( [ "function" ] , typeof props . regEx ) && ! ( props . regEx instanceof RegExp ) ) {
182+ if ( typeof props . regEx !== "undefined" && ! utils . contains ( [ "function" ] , typeof props . regEx ) && ! ( props . regEx instanceof RegExp ) ) {
184183 throw new TypeError ( `${ fieldName } .regEx must be a regular expression or function` ) ;
185184 }
186185
187186 // Check required
188- if ( typeof props . required !== "undefined" && ! SchemaUtils . contains ( [ "function" , "boolean" ] , typeof props . required ) ) {
187+ if ( typeof props . required !== "undefined" && ! utils . contains ( [ "function" , "boolean" ] , typeof props . required ) ) {
189188 throw new TypeError ( `${ fieldName } .required must be a boolean or function` ) ;
190189 }
191190 }
@@ -562,10 +561,9 @@ export class SchemaField {
562561 */
563562 validate ( value , options ) {
564563 const props = this . properties ;
565- const type = props . type ;
566564
567565 // Default options
568- options = SchemaUtils . extend ( {
566+ options = utils . extend ( {
569567 context : { [ this . name ] : value }
570568 } , options ) ;
571569
@@ -613,7 +611,7 @@ export class SchemaField {
613611 }
614612
615613 // Check type
616- switch ( type ) {
614+ switch ( props . type ) {
617615 case Array :
618616 if ( ! ( value instanceof Array ) ) {
619617 this . throwFieldTypeError ( label , "array" ) ;
@@ -668,10 +666,10 @@ export class SchemaField {
668666 break ;
669667
670668 default :
671- if ( type instanceof Schema ) {
672- type . validate ( value , options ) ;
669+ if ( props . type instanceof Schema ) {
670+ props . type . validate ( value , options ) ;
673671 }
674- else if ( type instanceof Array ) {
672+ else if ( props . type instanceof Array ) {
675673 // Check that value is an array
676674 if ( ! ( value instanceof Array ) ) {
677675 this . throwFieldTypeError ( label , "array" ) ;
@@ -680,7 +678,7 @@ export class SchemaField {
680678 else if ( value . length === 0 && ! isRequired ) {
681679 return value ;
682680 }
683- const arrayType = type [ 0 ] ;
681+ const arrayType = props . type [ 0 ] ;
684682
685683 // Validate array items
686684 if ( arrayType instanceof Schema ) {
@@ -741,9 +739,9 @@ export class SchemaField {
741739 }
742740 }
743741 }
744- else if ( typeof type === "function" ) {
742+ else if ( typeof props . type === "function" ) {
745743 // Check if value is an instance of the function
746- if ( ! ( value instanceof type ) ) {
744+ if ( ! ( value instanceof props . type ) ) {
747745 this . throwFieldInstanceError ( label ) ;
748746 }
749747 }
@@ -758,12 +756,12 @@ export class SchemaField {
758756
759757 if ( value instanceof Array ) {
760758 for ( let i = 0 ; i < value . length ; i += 1 ) {
761- if ( ! SchemaUtils . contains ( allowed , value [ i ] ) ) {
759+ if ( ! utils . contains ( allowed , value [ i ] ) ) {
762760 this . throwFieldBadValueError ( label ) ;
763761 }
764762 }
765763 }
766- else if ( ! SchemaUtils . contains ( allowed , value ) ) {
764+ else if ( ! utils . contains ( allowed , value ) ) {
767765 this . throwFieldBadValueError ( label ) ;
768766 }
769767 }
@@ -773,12 +771,12 @@ export class SchemaField {
773771
774772 if ( value instanceof Array ) {
775773 for ( let i = 0 ; i < value . length ; i += 1 ) {
776- if ( SchemaUtils . contains ( denied , value [ i ] ) ) {
774+ if ( utils . contains ( denied , value [ i ] ) ) {
777775 this . throwFieldDeniedValueError ( label ) ;
778776 }
779777 }
780778 }
781- else if ( SchemaUtils . contains ( denied , value ) ) {
779+ else if ( utils . contains ( denied , value ) ) {
782780 this . throwFieldDeniedValueError ( label ) ;
783781 }
784782 }
0 commit comments