function validate(message, messageTypeString, node, validateNode) {
  // eg message is a type of reaction, messageTypeString = 'Reaction'
  const xhr = new XMLHttpRequest();
  xhr.open('POST', '/dataset/proto/validate/' + messageTypeString);
  const binary = message.serializeBinary();
  if (!validateNode) {
    validateNode = $('.validate', node).first();
  }
  xhr.responseType = 'json';
  xhr.onload = function() {
    const validationOutput = xhr.response;
    const errors = validationOutput.errors;
    const warnings = validationOutput.warnings;
    // Add client-side validation errors.
    $(node).find('.invalid').each(function(index) {
      const invalidName = $(this).attr('class').split(' ')[0];
      errors.push('Value for ' + invalidName + ' is invalid');
    });
    const statusNode = $('.validate_status', validateNode);
    const messageNode = $('.validate_message', validateNode);
    statusNode.removeClass('fa-check');
    statusNode.removeClass('fa-exclamation-triangle');
    statusNode.css('backgroundColor', null);
    statusNode.text('');
    if (errors.length) {
      statusNode.addClass('fa fa-exclamation-triangle');
      statusNode.css('color', 'red');
      statusNode.text(' ' + errors.length);
      messageNode.html('<ul></ul>');
      for (let index = 0; index < errors.length; index++) {
        const error = errors[index];
        const errorNode = $('<li></li>');
        errorNode.text(error);
        $('ul', messageNode).append(errorNode);
      }
      messageNode.css('backgroundColor', 'pink');
    } else {
      statusNode.addClass('fa fa-check');
      statusNode.css('color', 'green');
      messageNode.html('');
      messageNode.css('backgroundColor', '');
      messageNode.css('visibility', 'hidden');
    }
    const warningStatusNode = $('.validate_warning_status', validateNode);
    const warningMessageNode = $('.validate_warning_message', validateNode);
    if (warnings.length) {
      warningStatusNode.show();
      warningStatusNode.text(' ' + warnings.length);
      warningMessageNode.show();
      warningMessageNode.html('<ul></ul>');
      for (let index = 0; index < warnings.length; index++) {
        const warning = warnings[index];
        const warningNode = $('<li></li>');
        warningNode.text(warning);
        $('ul', warningMessageNode).append(warningNode);
      }
    } else {
      warningStatusNode.hide();
      warningMessageNode.html('');
      warningMessageNode.hide();
    }
  };
  xhr.send(binary);
}
/**
 * Generic validator for many message types, not just reaction.
 * NOTE: This function does not commit or save anything!
 * @param {!jspb.Message} message The proto to validate.
 * @param {string} messageTypeString The message type.
 * @param {!Node} node Parent node for the unloaded message.
 * @param {?Node} validateNode Target div for validation output.
 */
Generic validator for many message types, not just reaction.
NOTE: This function does not commit or save anything.
@param {!jspb.Message} message The proto to validate.
@param {string} messageTypeString The message type.
@param {!Node} node Parent node for the unloaded message.
@param {?Node} validateNode Target div for validation output.
Generic validator for many message types not just reaction . NOTE : This function does not commit or save anything . @param { !jspb . Message } message The proto to validate . @param { string } messageTypeString The message type . @param { !Node } node Parent node for the unloaded message . @param { ?Node } validateNode Target div for validation output .
function validate(message, messageTypeString, node, validateNode) {
  const xhr = new XMLHttpRequest();
  xhr.open('POST', '/dataset/proto/validate/' + messageTypeString);
  const binary = message.serializeBinary();
  if (!validateNode) {
    validateNode = $('.validate', node).first();
  }
  xhr.responseType = 'json';
  xhr.onload = function() {
    const validationOutput = xhr.response;
    const errors = validationOutput.errors;
    const warnings = validationOutput.warnings;
    $(node).find('.invalid').each(function(index) {
      const invalidName = $(this).attr('class').split(' ')[0];
      errors.push('Value for ' + invalidName + ' is invalid');
    });
    const statusNode = $('.validate_status', validateNode);
    const messageNode = $('.validate_message', validateNode);
    statusNode.removeClass('fa-check');
    statusNode.removeClass('fa-exclamation-triangle');
    statusNode.css('backgroundColor', null);
    statusNode.text('');
    if (errors.length) {
      statusNode.addClass('fa fa-exclamation-triangle');
      statusNode.css('color', 'red');
      statusNode.text(' ' + errors.length);
      messageNode.html('<ul></ul>');
      for (let index = 0; index < errors.length; index++) {
        const error = errors[index];
        const errorNode = $('<li></li>');
        errorNode.text(error);
        $('ul', messageNode).append(errorNode);
      }
      messageNode.css('backgroundColor', 'pink');
    } else {
      statusNode.addClass('fa fa-check');
      statusNode.css('color', 'green');
      messageNode.html('');
      messageNode.css('backgroundColor', '');
      messageNode.css('visibility', 'hidden');
    }
    const warningStatusNode = $('.validate_warning_status', validateNode);
    const warningMessageNode = $('.validate_warning_message', validateNode);
    if (warnings.length) {
      warningStatusNode.show();
      warningStatusNode.text(' ' + warnings.length);
      warningMessageNode.show();
      warningMessageNode.html('<ul></ul>');
      for (let index = 0; index < warnings.length; index++) {
        const warning = warnings[index];
        const warningNode = $('<li></li>');
        warningNode.text(warning);
        $('ul', warningMessageNode).append(warningNode);
      }
    } else {
      warningStatusNode.hide();
      warningMessageNode.html('');
      warningMessageNode.hide();
    }
  };
  xhr.send(binary);
}
function validate ( message , messageTypeString , node , validateNode ) { const xhr = new XMLHttpRequest ( ) ; xhr . open ( 'POST' , '/dataset/proto/validate/' + messageTypeString ) ; const binary = message . serializeBinary ( ) ; if ( ! validateNode ) { validateNode = $ ( '.validate' , node ) . first ( ) ; } xhr . responseType = 'json' ; xhr . onload = function ( ) { const validationOutput = xhr . response ; const errors = validationOutput . errors ; const warnings = validationOutput . warnings ; $ ( node ) . find ( '.invalid' ) . each ( function ( index ) { const invalidName = $ ( this ) . attr ( 'class' ) . split ( ' ' ) [ 0 ] ; errors . push ( 'Value for ' + invalidName + ' is invalid' ) ; } ) ; const statusNode = $ ( '.validate_status' , validateNode ) ; const messageNode = $ ( '.validate_message' , validateNode ) ; statusNode . removeClass ( 'fa-check' ) ; statusNode . removeClass ( 'fa-exclamation-triangle' ) ; statusNode . css ( 'backgroundColor' , null ) ; statusNode . text ( '' ) ; if ( errors . length ) { statusNode . addClass ( 'fa fa-exclamation-triangle' ) ; statusNode . css ( 'color' , 'red' ) ; statusNode . text ( ' ' + errors . length ) ; messageNode . html ( '<ul></ul>' ) ; for ( let index = 0 ; index < errors . length ; index ++ ) { const error = errors [ index ] ; const errorNode = $ ( '<li></li>' ) ; errorNode . text ( error ) ; $ ( 'ul' , messageNode ) . append ( errorNode ) ; } messageNode . css ( 'backgroundColor' , 'pink' ) ; } else { statusNode . addClass ( 'fa fa-check' ) ; statusNode . css ( 'color' , 'green' ) ; messageNode . html ( '' ) ; messageNode . css ( 'backgroundColor' , '' ) ; messageNode . css ( 'visibility' , 'hidden' ) ; } const warningStatusNode = $ ( '.validate_warning_status' , validateNode ) ; const warningMessageNode = $ ( '.validate_warning_message' , validateNode ) ; if ( warnings . length ) { warningStatusNode . show ( ) ; warningStatusNode . text ( ' ' + warnings . length ) ; warningMessageNode . show ( ) ; warningMessageNode . html ( '<ul></ul>' ) ; for ( let index = 0 ; index < warnings . length ; index ++ ) { const warning = warnings [ index ] ; const warningNode = $ ( '<li></li>' ) ; warningNode . text ( warning ) ; $ ( 'ul' , warningMessageNode ) . append ( warningNode ) ; } } else { warningStatusNode . hide ( ) ; warningMessageNode . html ( '' ) ; warningMessageNode . hide ( ) ; } } ; xhr . send ( binary ) ; }
Generic validator for many message types, not just reaction.
Generic validator for many message types not just reaction .
// eg message is a type of reaction, messageTypeString = 'Reaction' // Add client-side validation errors.