var SchemaType = t.subtype(t.Obj, function (schema) {
    var availableTypes = keys(types);
    return (
        t.validate(schema.type, t.enums.of(availableTypes)).isValid() &&
        t.validate(schema, types[schema.type]).isValid()
    );
});
Ejemplo n.º 2
0
var validateBeer = function (beer) {
    ensure(
        new MW.Error(400, "Beer doesn't match the schema"),
        t.validate(beer, BeerType).isValid()
    );
    return beer;
};
Ejemplo n.º 3
0
    getValue: function (depth) {

      depth = depth || 0;

      var errors = [];
      var value = {};
      var result;
      
      for ( var i = 0 ; i < len ; i++ ) {
        var name = order[i];
        var result = this.refs[name].getValue(depth + 1);
        if (Result.is(result)) {
          errors = errors.concat(result.errors);
        } else {
          value[name] = result;
        }
      }
      if (errors.length) {
        return depth ? new Result({errors: errors}) : null;
      }

      result = t.validate(new Struct(value), type);
      var isValid = result.isValid();
      this.setState({hasError: !isValid});
      return isValid ? type(value) : depth ? result : null;
    },
Ejemplo n.º 4
0
 return function () {
   var value = rawValue || this.getRawValue();
   var result = t.validate(value, type);
   var isValid = result.isValid();
   this.setState({hasError: !isValid});
   return isValid ? type(value) : result;
 };
Ejemplo n.º 5
0
    getValue: function (depth) {

      depth = depth || 0;

      var errors = [];
      var value = [];
      var result;
      
      for ( var i = 0, len = this.state.value.length ; i < len ; i++ ) {
        var result = this.refs[i].getValue(depth + 1);
        if (Result.is(result)) {
          errors = errors.concat(result.errors);
        } else {
          value.push(result);
        }
      }
      if (errors.length) {
        return depth ? new Result({errors: errors}) : null;
      }

      result = t.validate(value, type);
      var isValid = result.isValid();
      this.setState({hasError: !isValid, value: value});
      return isValid ? type(value) : depth ? result : null;
    },
 it("invalid `allowedValues` property", function () {
     var schema = {
         type: "string",
         allowedValues: ["value_0", "value_1", 0]
     };
     t.validate(schema, SchemaType).isValid().should.equal(false);
 });
Ejemplo n.º 7
0
  validate() {
    let value = {};
    let errors = [];
    let hasError = false;
    let result;

    if (this.typeInfo.isMaybe && this.isValueNully()) {
      this.removeErrors();
      return new t.ValidationResult({errors: [], value: null});
    }

    for (const ref in this.refs) {
      if (this.refs.hasOwnProperty(ref)) {
        result = this.refs[ref].validate();
        errors = errors.concat(result.errors);
        value[ref] = result.value;
      }
    }

    if (errors.length === 0) {
      const InnerType = this.typeInfo.innerType;
      value = new InnerType(value);
      if (this.typeInfo.isSubtype && errors.length === 0) {
        result = t.validate(value, this.props.type, this.getValidationOptions());
        hasError = !result.isValid();
        errors = errors.concat(result.errors);
      }
    }

    this.setState({hasError: hasError});
    return new t.ValidationResult({errors, value});
  }
Ejemplo n.º 8
0
  validate() {

    var value = {};
    var errors = [];
    var hasError = false;
    var result;

    for (var ref in this.refs) {
      if (this.refs.hasOwnProperty(ref)) {
        result = this.refs[ref].getValue();
        errors = errors.concat(result.errors);
        value[ref] = result.value;
      }
    }

    if (errors.length === 0) {
      value = new this.typeInfo.innerType(value);
      if (this.typeInfo.isSubtype && errors.length === 0) {
        result = t.validate(value, this.props.type, this.props.ctx.path);
        hasError = !result.isValid();
        errors = errors.concat(result.errors);
      }
    }

    return new t.ValidationResult({errors, value});
  }
Ejemplo n.º 9
0
const findErrors = (state) => {
  const validacija = tv.validate(state, ModelView);
  const validno = validacija.isValid();
  const prvaGreska = validacija.firstError();
  const nizGresaka = validacija.errors;
  return nizGresaka;
}
Ejemplo n.º 10
0
  validate() {
    const value = [];
    let errors = [];
    let hasError = false;
    let result;

    if (this.typeInfo.isMaybe && this.isValueNully()) {
      this.removeErrors();
      return new t.ValidationResult({errors: [], value: null});
    }

    for (let i = 0, len = this.state.value.length; i < len; i++ ) {
      result = this.refs[i].validate();
      errors = errors.concat(result.errors);
      value.push(result.value);
    }

    // handle subtype
    if (this.typeInfo.isSubtype && errors.length === 0) {
      result = t.validate(value, this.props.type, this.getValidationOptions());
      hasError = !result.isValid();
      errors = errors.concat(result.errors);
    }

    this.setState({hasError: hasError});
    return new t.ValidationResult({errors: errors, value: value});
  }
Ejemplo n.º 11
0
 return (...args) => {
     for (var i = 0; i < types.length; i++) {
         const error = validate(args[i], types[i]).firstError();
         if (error) {
             throw new Error(error.message);
         }
     }
 };
Ejemplo n.º 12
0
 sanityCheck() {
   const v = t.validate(this, RequiredConfigType);
   if(!v.isValid()) {
     console.error(`${v.errors.length} errors during config validation.`);
     _.each(v.errors, ({ message }) => console.error(message));
     console.error('Check your configuration and https://goo.gl/MTvM8w for help.');
     throw v.firstError();
   }
 }
 it("simple list", function () {
     var schema = {
         type: "list",
         of: {
             type: "string"
         }
     };
     t.validate(schema, SchemaType).isValid().should.equal(true);
 });
 it("invalid `shape` property", function () {
     var schema = {
         type: "structure",
         shape: {
             notASchema: "notASchema"
         }
     };
     t.validate(schema, SchemaType).isValid().should.equal(false);
 });
 it("invalid `of` property", function () {
     var schema = {
         type: "list",
         of: {
             notASchema: "notASchema"
         }
     };
     t.validate(schema, SchemaType).isValid().should.equal(false);
 });
Ejemplo n.º 16
0
 Union.dispatch = function (x) {
   var ctor = null;
   for (var i = 0; i < types.length; i++) {
     if (validate(x, types[i]).isValid()) {
       ctor = types[i];
       break;
     }
   }
   return ctor;
 };
Ejemplo n.º 17
0
function onSubmit(e, dispatch) {
  e.preventDefault()
  const form = serialize(e.target)
  const gathering = postGathering(form.name, form.location, form.description, form.date, form.time)
  const result = validate(gathering, Gathering)
  
  if(result.isValid()) dispatch(UiDidCreateGathering(gathering))
  else console.log(result.errors)

}
 it("simple structure", function () {
     var schema = {
         type: "structure",
         shape: {
             prop: {
                 type: "string"
             }
         }
     };
     t.validate(schema, SchemaType).isValid().should.equal(true);
 });
Ejemplo n.º 19
0
 const getValidationErrorMessage = innerGetValidationErrorMessage ? (value, path, context) => {
   const result = t.validate(value, type, {path, context});
   if (!result.isValid()) {
     for (let i = 0, len = result.errors.length; i < len; i++ ) {
       if (t.Function.is(result.errors[i].expected.getValidationErrorMessage)) {
         return result.errors[i].message;
       }
     }
     return innerGetValidationErrorMessage(value, path, context);
   }
 } : undefined;
Ejemplo n.º 20
0
 return function parseBody(req, res, cb) {
   let data = parseTyped(type, req.body);
   let result = validate(data, type);
   if (result.isValid()) {
     return cb();
   } else {
     if (process.env.NODE_ENV != "testing") {
       logger.error(result.errors);
     }
     return res.status(400).sendFile(Path.join(PUBLIC_DIR, "errors/400.html"));
   }
 };
Ejemplo n.º 21
0
 return function parseQuery(req, res, cb) {
   req.query = jsonApi.parseQuery(req.query)
   let data = parseTyped(type, req.query)
   let result = validate(data, type)
   if (result.isValid()) {
     return cb()
   } else {
     if (process.env.NODE_ENV != "testing") {
       logger.error(result.errors)
     }
     return res.status(400).sendFile(Path.join(PUBLIC_DIR, "errors/400.html"))
   }
 }
Ejemplo n.º 22
0
 addProject(data) {
   data.id = uuid()
   data.modified = new Date()
   data.pluginData = data.pluginData || {}
   data.plugins = data.plugins || {}
   const val = validate(data, Project)
   if (!val.isValid()) {
     this.logger.warn(JSON.stringify(val, null, 2))
     this.logger.warn(val.errors)
     return Promise.reject(
       new Error('Invalid project data: \n' +
         val.errors.map(e => e.message).join('\n')))
   }
   return this.db.put('projects', data.id, data)
     .then(() => data)
 }
Ejemplo n.º 23
0
 .then(project => {
   for (let name in data) {
     project[name] = data[name]
   }
   project.modified = new Date()
   project.pluginData = project.pluginData || {}
   const val = validate(project, Project)
   if (!val.isValid()) {
     this.logger.warn(JSON.stringify(val, null, 2))
     return Promise.reject(
       new Error('Invalid project data: \n' +
         val.errors.map(e => e.message).join('\n')))
   }
   return this.db.put('projects', id, project)
     .then(() => {
       this.io.emit('project:update', project)
       return project
     })
 })
Ejemplo n.º 24
0
      checkPropType = function (values, prop, displayName) {

        var value = values[prop];
        var validationResult = t.validate(value, propType);

        if (!validationResult.isValid()) {

          var message = [
            'Invalid prop ' + t.stringify(prop) + ' supplied to ' + displayName + ', should be a ' + t.getTypeName(propType) + '.\n',
            'Detected errors (' + validationResult.errors.length + '):\n'
          ].concat(validationResult.errors.map(function (e, i) {
            return ' ' + (i + 1) + '. ' + e.message;
          })).join('\n') + '\n\n';

          // add a readable entry in the call stack
          // when "Pause on exceptions" and "Pause on Caught Exceptions"
          // are enabled in Chrome DevTools
          checkPropType.displayName = message;

          t.fail(message);
        }
      };
Ejemplo n.º 25
0
  getValue: function () {
    var report = this.props.ctx.report;
    var value = [];
    var errors = [];
    var hasError = false;
    var result;

    for (var i = 0, len = this.state.value.length ; i < len ; i++ ) {
      result = this.refs[i].getValue();
      errors = errors.concat(result.errors);
      value.push(result.value);
    }

    // handle subtype
    if (report.subtype && errors.length === 0) {
      result = t.validate(value, report.type, this.props.ctx.path);
      hasError = !result.isValid();
      errors = errors.concat(result.errors);
    }

    this.setState({hasError: hasError});
    return new t.ValidationResult({errors: errors, value: value});
  },
Ejemplo n.º 26
0
 getValue: function () {
   var value = this.getTransformer().parse(this.state.value);
   var result = t.validate(value, this.props.ctx.report.type, this.props.ctx.path);
   this.setState({hasError: !result.isValid()});
   return result;
 },
Ejemplo n.º 27
0
 validate() {
   var value = this.getTransformer().parse(this.state.value);
   return t.validate(value, this.props.type, this.props.ctx.path);
 }
Ejemplo n.º 28
0
 pureValidate() {
   return t.validate(this.getValue(), this.props.type, this.getValidationOptions());
 }
Ejemplo n.º 29
0
 validate() {
   var result = t.validate(this.getValue(), this.props.type, this.getValidationOptions());
   this.setState({hasError: !result.isValid()});
   return result;
 }