示例#1
0
文件: index.js 项目: maxdec/cellar
import validate from 'validate.js';
import fetch from 'isomorphic-fetch';

validate.extend(validate.validators.datetime, {
  parse: function(value) {
    return new Date(value).getTime();
  },
  format: function(value) {
    return new Date(value).toISOString().slice(0, 10);
  }
});

const wineConstraints = {
  name: { presence: true },
  designation: { presence: true },
  vintage: { presence: true, format: /^\d{4}$/ },
  readyToDrink: { presence: true, format: /^\d{4}(-\d{4})?$/ },
  color: { presence:true, inclusion: ['red', 'white', 'rose']},
};

const bottleConstraints = {
  // wine: { presence: true, numericality: { onlyInteger: true, greaterThan: 0 }},
  acquisition: { presence: true, date: true },
  degustation: { date: true },
  row: { presence: true, numericality: { onlyInteger: true, greaterThanOrEqualTo: 0, lessThan: 6, }},
  col: { presence: true, numericality: { onlyInteger: true, greaterThanOrEqualTo: 0, lessThan: 4, }},
  rating: { numericality: { onlyInteger: true, greaterThanOrEqualTo: 0, lessThanOrEqualTo: 5, }},
  price: { numericality: { onlyInteger: true, greaterThanOrEqualTo: 0, }},
};

export const validateWine = (wine) => (validate(wine, wineConstraints));
示例#2
0
import validate from 'validate.js';
import moment from 'moment';

validate.extend(validate.validators.datetime, {
  // The value is guaranteed not to be null or undefined but otherwise it
  // could be anything.
  parse(value) {
    return +moment.utc(value);
  },
  // Input is a unix timestamp
  format(value, options) {
    const format = options.dateOnly ? 'YYYY-MM-DD' : 'YYYY-MM-DD hh:mm:ss';
    return moment.utc(value).format(format);
  },
});

validate.validators.presence.options = { message: 'is required' };

export default class FormValidator {
  constructor(schema) {
    this.schema = schema;
    this.errors = {};
  }

  validateAll(values) {
    this.errors = validate.validate(values, this.schema);
    return this.errors;
  }

  validateField(field, value) {
    const validationField = {};
示例#3
0
  return validateFxoObject('datetime', value, options);
};

validate.validators.fxoBoolean = function(value, options) {
  return validateFxoObject('boolean', value, options);
};

validate.extend(validate.validators.datetime, {
  // The value is guaranteed not to be null or undefined
  // but otherwise it could be anything.
  parse: function(value) {
    var d = FxoUtils.parseDateTimeField(value);
    return d.valid ? d.parsed.getTime() : NaN;
  },
  // Input is a unix timestamp
  format: function(value, options) {
    var format = options.dateOnly ?
      '{yyyy}-{MM}-{dd}' :
      '{yyyy}-{MM}-{dd} {hh}:{mm}:{ss}';

    var d = FxoUtils.parseDateTimeField(value);
    return d.parsed.format(format);
  }
});

validate.options = validate.async.options = {
  format: 'flat',
  fullMessages: true
};

module.exports = validate;