<commit_before>var Command = require('peasy-js').Command;
var CanShipOrderItemRule = require('../rules/canShipOrderItemRule');
var CanDeleteProductRule = require('../rules/canDeleteProductRule');
var utils = require('../shared/utils');
var FieldRequiredRule = require('../rules/fieldRequiredRule');
var FieldLengthRule = require('../rules/fieldLengthRule');
var FieldTypeRule = require('../rules/fieldTypeRule');

var CreateProductCommand = Command.extend({
  params: ['product', 'productDataProxy', 'inventoryItemService'],
  functions: {
    _onInitialization: function(context, done) {
      var product = this.product;
      utils.stripAllFieldsFrom(product).except(['name', 'description', 'price', 'categoryId']);
      done();
    },
    _getRules: function(context, done) {
      var product = this.product;
      done(null, [
        new FieldRequiredRule("name", product)
             .ifValidThenValidate(new FieldLengthRule("name", product.name, 50)),
        new FieldRequiredRule("price", product)
             .ifValidThenValidate(new FieldTypeRule("price", product.price, "number")),
        new FieldRequiredRule("categoryId", product)
      ]);<commit_msg>add logic to parse incoming entity values<commit_after>var Command = require('peasy-js').Command;
var CanShipOrderItemRule = require('../rules/canShipOrderItemRule');
var CanDeleteProductRule = require('../rules/canDeleteProductRule');
var utils = require('../shared/utils');
var FieldRequiredRule = require('../rules/fieldRequiredRule');
var FieldLengthRule = require('../rules/fieldLengthRule');
var FieldTypeRule = require('../rules/fieldTypeRule');

var convert = function(value, prop) {
  function toFloat() {
    var parsed = parseFloat(value[prop]);
    if (parsed) {
      value[prop] = parsed;
    } 
  }

  function toInt() {
    var parsed = parseInt(value[prop]);
    if (parsed) {
      value[prop] = parsed;
    } 
  }

  return {
    toFloat: toFloat,
    toInt: toInt
  };
}

var CreateProductCommand = Command.extend({
  params: ['product', 'productDataProxy', 'inventoryItemService'],
  functions: {
    _onInitialization: function(context, done) {
      var product = this.product;
      convert(product, "price").toFloat();
      convert(product, "categoryId").toInt();
      utils.stripAllFieldsFrom(product).except(['name', 'description', 'price', 'categoryId']);
      done();
    },
    _getRules: function(context, done) {
      var product = this.product;
      done(null, [
        new FieldRequiredRule("name", product)
             .ifValidThenValidate(new FieldLengthRule("name", product.name, 50)),
        new FieldRequiredRule("price", product)
             .ifValidThenValidate(new FieldTypeRule("price", product.price, "number")),
        new FieldRequiredRule("categoryId", product)
             .ifValidThenValidate(new FieldTypeRule("categoryId", product.categoryId, "number")),
      ]);
{'fraction_non_alphanumeric': 0.14798206278026907, 'fraction_numerical': 0.0013797861331493618, 'mean_word_length': 3.974271012006861, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1781800', 'n_tokens_mistral': 804, 'n_tokens_neox': 809, 'n_words': 169}