jenkins-bot has submitted this change and it was merged.
Change subject: [time.js] enhancement of time.Time.validate, making sure
required fields for given precision are set
......................................................................
[time.js] enhancement of time.Time.validate, making sure required fields for
given precision are set
Change-Id: I12b49727ca1c737afa76569a70c3f35234735c3d
---
M DataValues/resources/time.js/src/time.Time.validate.js
M DataValues/resources/time.js/tests/time.Time.validate.tests.js
2 files changed, 92 insertions(+), 8 deletions(-)
Approvals:
Henning Snater: Looks good to me, approved
jenkins-bot: Verified
diff --git a/DataValues/resources/time.js/src/time.Time.validate.js
b/DataValues/resources/time.js/src/time.Time.validate.js
index 79d1a2d..2dd8511 100644
--- a/DataValues/resources/time.js/src/time.Time.validate.js
+++ b/DataValues/resources/time.js/src/time.Time.validate.js
@@ -26,13 +26,12 @@
precision: 'number'
} );
- if( definition.year === undefined || isNaN( definition.year ) )
{
- throw new Error( '"year" has to be a number' );
- }
+ checkPrecisionRequirements( definition );
- if( definition.month > 12 || definition.month < 1 ) {
+ var month = definition.month;
+ if( month > 12 || month < 1 ) {
throw new Error( '"month" must not be lower than 1
(January) or higher than 12 ' +
- '(December). "' + definition.month + '" is not
a valid month number.' );
+ '(December). "' + month + '" is not a valid
month number.' );
}
if( definition.day < 1 ) {
@@ -47,11 +46,47 @@
throw new Error( '"calendarname" is "' +
definition.calendarname + '" but has to be "'
+ Time.CALENDAR.GREGORIAN + '" or "' +
Time.CALENDAR.JULIAN + '"' );
}
+ };
- if( !Time.knowsPrecision( definition.precision ) ) {
+ /**
+ * Makes sure a given structure has a proper precision set by
validating the precision itself
+ * and checking if all fields required by that precision are set. E.g.
if precision is "MONTH",
+ * then also the field "year" has to be given.
+ *
+ * @param {Object} definition
+ * @throws {Error}
+ */
+ function checkPrecisionRequirements( definition ) {
+ var precision = definition.precision,
+ year = definition.year;
+
+ if( !Time.knowsPrecision( precision ) ) {
throw new Error( 'Unknown precision "' +
definition.precision + '" given in "precision"' );
}
- };
+
+ // make sure fields with time information required for given
precision are set:
+ if( precision > Time.PRECISION.DAY ) {
+ throw new Error( 'Precision higher than "DAY" is not
yet supported' );
+ }
+ if( precision >= Time.PRECISION.DAY
+ && !definition.day
+ ) {
+ throw new Error( 'Field "day" required because
precision is "DAY' );
+ }
+ if( precision >= Time.PRECISION.MONTH
+ && !definition.month
+ ) {
+ throw new Error( 'Field "month" required because
precision is "MONTH' );
+ }
+
+ // year is always required
+ if( year === undefined
+ || isNaN( year )
+ || !isFinite( year )
+ ) {
+ throw new Error( '"year" has to be a finite number' );
+ }
+ }
/**
* Checks a definition for certain fields. If the field is available,
an error will be thrown
diff --git a/DataValues/resources/time.js/tests/time.Time.validate.tests.js
b/DataValues/resources/time.js/tests/time.Time.validate.tests.js
index 97c5240..24d175f 100644
--- a/DataValues/resources/time.js/tests/time.Time.validate.tests.js
+++ b/DataValues/resources/time.js/tests/time.Time.validate.tests.js
@@ -129,7 +129,56 @@
newInvalidTestDefinition(
'unknown calendar name',
{ calendarname: 'foo' }
- )
+ ),
+ newInvalidTestDefinition(
+ 'precision higher day not yet supported',
+ { precision: Time.PRECISION.DAY + 1 }
+ ), {
+ reason: 'precision is "DAY" but field "day" not given',
+ definition: {
+ calendarname: J,
+ year: 1492,
+ month: 10,
+ precision: PRECISION.DAY
+ }
+ }, {
+ reason: 'precision is "DAY" but field "month" not
given',
+ definition: {
+ calendarname: J,
+ year: 1492,
+ day: 1,
+ precision: PRECISION.DAY
+ }
+ }, {
+ reason: 'precision is "DAY" but field "year" not given',
+ definition: {
+ calendarname: J,
+ month: 1,
+ day: 1,
+ precision: PRECISION.DAY
+ }
+ }, {
+ reason: 'precision is "MONTH" but field "month" not
given',
+ definition: {
+ calendarname: J,
+ year: 1234,
+ precision: PRECISION.MONTH
+ }
+ }, {
+ reason: 'precision is "MONTH" but field "year" not
given',
+ definition: {
+ calendarname: J,
+ month: 12,
+ precision: PRECISION.MONTH
+ }
+ }, {
+ reason: 'precision is "YEAR" but field "year" not
given',
+ definition: {
+ calendarname: J,
+ month: 12,
+ precision: PRECISION.YEAR
+ }
+ }
];
QUnit.test( 'validating invalid time definitions', function( assert ) {
--
To view, visit https://gerrit.wikimedia.org/r/63991
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I12b49727ca1c737afa76569a70c3f35234735c3d
Gerrit-PatchSet: 7
Gerrit-Project: mediawiki/extensions/DataValues
Gerrit-Branch: master
Gerrit-Owner: Daniel Werner <[email protected]>
Gerrit-Reviewer: Henning Snater <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits