Permalink
Browse files

Various: Validate parameters presence, type and value

Ref #251
Ref #268
  • Loading branch information...
rxaviers committed May 31, 2014
1 parent 65a109b commit abf5b30048d041161c165e9f8dcbde22aafc17db
View
@@ -13,4 +13,4 @@
"unused": true,
"node": true
-}
+}
View
@@ -30,3 +30,9 @@
factory( root.Cldr, root.Globalize );
}
}(this, function( Cldr, Globalize ) {
+
+var createError = Globalize._createError,
+ formatMessage = Globalize._formatMessage,
+ isPlainObject = Globalize._isPlainObject,
+ validatePresence = Globalize._validatePresence,
+ validateType = Globalize._validateType;
@@ -26,3 +26,7 @@
factory( root.Cldr, root.Globalize );
}
}(this, function( Cldr, Globalize ) {
+
+var validatePresence = Globalize._validatePresence,
+ validateType = Globalize._validateType,
+ validateTypePlainObject = Globalize._validateTypePlainObject;
@@ -29,3 +29,8 @@
factory( root.Cldr, root.Globalize );
}
}(this, function( Cldr, Globalize ) {
+
+var objectKeys = Globalize._objectKeys,
+ validatePresence = Globalize._validatePresence,
+ validateType = Globalize._validateType,
+ validateTypePlainObject = Globalize._validateTypePlainObject;
View
@@ -0,0 +1,22 @@
+define([
+ "./format-message",
+ "../util/array/for-each",
+ "../util/object/keys"
+], function( formatMessage, arrayForEach, objectKeys ) {
+
+return function( code, message, attributes ) {
+ var error;
+
+ message = code + ( message ? ": " + formatMessage( message, attributes ) : "" );
+ error = new Error( message );
+ error.code = code;
+
+ // extend( error, attributes );
+ arrayForEach( objectKeys( attributes ), function( attribute ) {
+ error[ attribute ] = attributes[ attribute ];
+ });
+
+ return error;
+};
+
+});
@@ -0,0 +1,12 @@
+define([
+ "../create-error"
+], function( createError ) {
+
+return function( name, value ) {
+ return createError( "E_INVALID_PAR_VALUE", "Invalid `{name}` value ({value}).", {
+ name: name,
+ value: value
+ });
+};
+
+});
View
@@ -0,0 +1,11 @@
+define([
+ "./create-error"
+], function( createError ) {
+
+return function( code, message, check, attributes ) {
+ if ( !check ) {
+ throw createError( code, message, attributes );
+ }
+};
+
+});
@@ -0,0 +1,11 @@
+define([
+ "../validate"
+], function( validate ) {
+
+return function( value, name ) {
+ validate( "E_MISSING_PARAMETER", "Missing required parameter `{name}`.", typeof value !== "undefined", {
+ name: name
+ });
+};
+
+});
@@ -0,0 +1,13 @@
+define([
+ "../validate"
+], function( validate ) {
+
+return function( value, name, check, expected ) {
+ validate( "E_INVALID_PAR_TYPE", "Invalid `{name}` parameter ({value}). {expected} expected.", check, {
+ expected: expected,
+ name: name,
+ value: value
+ });
+};
+
+});
@@ -0,0 +1,10 @@
+define([
+ "../type",
+ "../../../util/array/is-array"
+], function( validateType, arrayIsArray ) {
+
+return function( value, name ) {
+ validateType( value, name, typeof value === "undefined" || arrayIsArray( value ), "Array" );
+};
+
+});
@@ -0,0 +1,10 @@
+define([
+ "../type",
+ "../../../util/is-plain-object"
+], function( validateType, isPlainObject ) {
+
+return function( value, name ) {
+ validateType( value, name, typeof value === "undefined" || typeof value === "string" || isPlainObject( value ), "String or plain Object" );
+};
+
+});
@@ -0,0 +1,9 @@
+define([
+ "../type"
+], function( validateType ) {
+
+return function( value, name ) {
+ validateType( value, name, typeof value === "undefined" || value instanceof Date, "Date" );
+};
+
+});
@@ -0,0 +1,10 @@
+define([
+ "cldr",
+ "../type"
+], function( Cldr, validateType ) {
+
+return function( value, name ) {
+ validateType( value, name, typeof value === "undefined" || typeof value === "string" || value instanceof Cldr, "String or Cldr instance" );
+};
+
+});
@@ -0,0 +1,9 @@
+define([
+ "../type"
+], function( validateType ) {
+
+return function( value, name ) {
+ validateType( value, name, typeof value === "undefined" || typeof value === "number", "Number" );
+};
+
+});
@@ -0,0 +1,10 @@
+define([
+ "../type",
+ "../../../util/is-plain-object"
+], function( validateType, isPlainObject ) {
+
+return function( value, name ) {
+ validateType( value, name, typeof value === "undefined" || isPlainObject( value ), "Plain Object" );
+};
+
+});
@@ -0,0 +1,9 @@
+define([
+ "../type"
+], function( validateType ) {
+
+return function( value, name ) {
+ validateType( value, name, typeof value === "undefined" || typeof value === "string", "a string" );
+};
+
+});
View
@@ -1,7 +1,15 @@
define([
"cldr",
- "./util/always-cldr"
-], function( Cldr, alwaysCldr ) {
+ "./common/create-error",
+ "./common/format-message",
+ "./common/validate/presence",
+ "./common/validate/type",
+ "./common/validate/type/locale",
+ "./common/validate/type/plain-object",
+ "./util/always-cldr",
+ "./util/is-plain-object",
+ "./util/object/keys"
+], function( Cldr, createError, formatMessage, validatePresence, validateType, validateTypeLocale, validateTypePlainObject, alwaysCldr, isPlainObject, objectKeys ) {
/**
* [new] Globalize( locale|cldr )
@@ -17,9 +25,8 @@ function Globalize( locale ) {
return new Globalize( locale );
}
- if ( !locale ) {
- throw new Error( "Missing locale" );
- }
+ validatePresence( locale, "locale" );
+ validateTypeLocale( locale, "locale" );
this.cldr = alwaysCldr( locale );
}
@@ -33,6 +40,9 @@ function Globalize( locale ) {
* Somewhat equivalent to previous Globalize.addCultureInfo(...).
*/
Globalize.load = function( json ) {
+ validatePresence( json, "json" );
+ validateTypePlainObject( json, "json" );
+
Cldr.load( json );
};
@@ -48,12 +58,25 @@ Globalize.load = function( json ) {
* Return the default Cldr instance.
*/
Globalize.locale = function( locale ) {
+ validateTypeLocale( locale, "locale" );
+
if ( arguments.length ) {
this.cldr = alwaysCldr( locale );
}
return this.cldr;
};
+/**
+ * Optimization to avoid duplicating some internal functions across modules.
+ */
+Globalize._createError = createError;
+Globalize._formatMessage = formatMessage;
+Globalize._isPlainObject = isPlainObject;
+Globalize._objectKeys = objectKeys;
+Globalize._validatePresence = validatePresence;
+Globalize._validateTypePlainObject = validateTypePlainObject;
+Globalize._validateType = validateType;
+
return Globalize;
});
View
@@ -1,13 +1,18 @@
define([
"cldr",
+ "./common/validate/presence",
+ "./common/validate/type",
+ "./common/validate/type/date",
+ "./common/validate/type/date-pattern",
+ "./common/validate/type/string",
"./core",
"./date/all-presets",
"./date/expand-pattern",
"./date/format",
"./date/parse",
"./util/always-array",
"cldr/supplemental"
-], function( Cldr, Globalize, dateAllPresets, dateExpandPattern, dateFormat, dateParse, alwaysArray ) {
+], function( Cldr, validatePresence, validateTypeDataType, validateTypeDate, validateTypeDatePattern, validateTypeString, Globalize, dateAllPresets, dateExpandPattern, dateFormat, dateParse, alwaysArray ) {
/**
* .formatDate( value, pattern )
@@ -22,15 +27,13 @@ Globalize.formatDate =
Globalize.prototype.formatDate = function( value, pattern ) {
var cldr;
- if ( !( value instanceof Date ) ) {
- throw new Error( "Value is not date" );
- }
-
- if ( !pattern ) {
- throw new Error( "Missing pattern" );
- }
+ validatePresence( value, "value" );
+ validatePresence( pattern, "pattern" );
+ validateTypeDate( value, "value" );
+ validateTypeDatePattern( pattern, "pattern" );
cldr = this.cldr;
+
pattern = dateExpandPattern( pattern, cldr );
return dateFormat( value, pattern, cldr );
};
@@ -48,9 +51,8 @@ Globalize.parseDate =
Globalize.prototype.parseDate = function( value, patterns ) {
var cldr, date;
- if ( typeof value !== "string" ) {
- throw new Error( "invalid value (" + value + "), string expected" );
- }
+ validatePresence( value, "value" );
+ validateTypeString( value, "value" );
cldr = this.cldr;
@@ -61,6 +63,7 @@ Globalize.prototype.parseDate = function( value, patterns ) {
}
patterns.some(function( pattern ) {
+ validateTypeDatePattern( pattern, "patterns" );
pattern = dateExpandPattern( pattern, cldr );
date = dateParse( value, pattern, cldr );
return !!date;
Oops, something went wrong.

0 comments on commit abf5b30

Please sign in to comment.