Robmoen has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/171023

Change subject: Validate claims in WikiGrokResponseApi
......................................................................

Validate claims in WikiGrokResponseApi

Change-Id: I455d383a76e6e7bf0f17dd6d7c4c652795d44d35
---
M javascripts/modules/wikigrok/WikiGrokDialog.js
M javascripts/modules/wikigrok/WikiGrokResponseApi.js
2 files changed, 45 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/23/171023/1

diff --git a/javascripts/modules/wikigrok/WikiGrokDialog.js 
b/javascripts/modules/wikigrok/WikiGrokDialog.js
index cd03628..2bee62f 100644
--- a/javascripts/modules/wikigrok/WikiGrokDialog.js
+++ b/javascripts/modules/wikigrok/WikiGrokDialog.js
@@ -52,7 +52,7 @@
                                version: this.version
                        } );
                        this.apiWikiGrokResponse = new WikiGrokResponseApi( {
-                               itemId: options.itemId,
+                               subjectId: options.itemId,
                                subject: options.name,
                                version: this.version,
                                userToken: options.userToken,
diff --git a/javascripts/modules/wikigrok/WikiGrokResponseApi.js 
b/javascripts/modules/wikigrok/WikiGrokResponseApi.js
index 77af849..0596fd6 100644
--- a/javascripts/modules/wikigrok/WikiGrokResponseApi.js
+++ b/javascripts/modules/wikigrok/WikiGrokResponseApi.js
@@ -1,5 +1,5 @@
 // jscs:disable requireCamelCaseOrUpperCaseIdentifiers
-( function ( M ) {
+( function ( M, $ ) {
        var Api = M.require( 'api' ).Api, WikiGrokResponseApi;
        /**
         * Record claims to the WikiGrok api
@@ -8,7 +8,7 @@
        WikiGrokResponseApi = Api.extend( {
 
                initialize: function ( options ) {
-                       this.subjectId = options.itemId;
+                       this.subjectId = options.subjectId;
                        this.subject = options.subject;
                        this.userToken = options.userToken;
                        this.taskType = 'mobile ' + options.version;
@@ -17,12 +17,52 @@
                        Api.prototype.initialize.apply( this, arguments );
                },
                /**
-                * Saves claims to the wikigrok api server
+                * Validates and saves claims to the wikigrok api server
                 * @method
                 * @param {Array} claims a list of claims. Each claim must have 
correct, prop, propid, value and valueid set
                 * @return {jQuery.Deferred}
                 */
                recordClaims: function ( claims ) {
+                       if ( $.type( Number( this.subjectId.slice( 1 ) ) ) !== 
'number' ) {
+                               throw 'WikiGrokResponseApi: subjectId must be a 
number';
+                       }
+                       if ( $.type( this.subject ) !== 'string' ) {
+                               throw 'WikiGrokResponseApi: subject must be a 
string';
+                       }
+                       if ( $.type( this.userToken ) !== 'string' ) {
+                               throw 'WikiGrokResponseApi: userToken must be a 
string';
+                       }
+                       if ( $.type( this.taskType ) !== 'string' ) {
+                               throw 'WikiGrokResponseApi: taskType must be a 
string';
+                       }
+                       if ( $.type( this.taskType ) !== 'string' ) {
+                               throw 'WikiGrokResponseApi: taskToken must be a 
string';
+                       }
+                       if ( $.type( this.testing ) !== 'boolean' ) {
+                               throw 'WikiGrokResponseApi: testing must be a 
boolean';
+                       }
+                       if ( $.isArray( claims ) ) {
+                               $.each( claims, function ( i, claim ) {
+                                       console.log( i, claim );
+                                       if ( $.type( claim ) === 'object' ) {
+                                               if ( claim.correct ) {
+                                                       if ( $.type( 
claim.correct ) !== 'boolean' ) {
+                                                               throw 
'WikiGrokResponseApi: claim.correct must be a boolean';
+                                                       }
+                                               } else {
+                                                       throw 
'WikiGrokResponseApi: claim.correct must be set';
+                                               }
+                                               if ( !claim.correct || 
!claim.prop || !claim.propid || !claim.value || !claim.valueid ) {
+                                                       throw 
'WikiGrokResponseApi: Not a valid claim object:' + JSON.stringify( claim );
+                                               }
+
+                                       } else {
+                                               throw 'WikiGrokResponseApi: 
claim must be an object';
+                                       }
+                               } );
+                       } else {
+                               throw 'WikiGrokResponseApi: claims must be an 
array';
+                       }
                        return this.postWithToken( 'edit', {
                                action: 'wikigrokresponse',
                                page_id: mw.config.get( 'wgArticleId' ),
@@ -41,4 +81,4 @@
        } );
        M.define( 'modules/wikigrok/WikiGrokResponseApi', WikiGrokResponseApi );
 
-}( mw.mobileFrontend ) );
+}( mw.mobileFrontend, jQuery ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/171023
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I455d383a76e6e7bf0f17dd6d7c4c652795d44d35
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Robmoen <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to