Ejemplo n.º 1
0
  bodyParam: function (paramName, attributes) {
    'use strict';
    var type = attributes.type,
      description = attributes.description,
      allowInvalid = attributes.allowInvalid,
      cfg, construct;

    if (attributes.isJoi) {
      type = attributes;
      description = undefined;
      allowInvalid = undefined;
    }

    if (!type) {
      construct = function (raw) {
        return raw;
      };
    } else if (typeof type === 'function' || is.array(type)) {
      // assume ModelOrSchema is a Foxx Model
      construct = createModelInstantiator(type, allowInvalid);
    } else {
      if (!type.isJoi) {
        type = joi.object().keys(type).required();
      }
      if (typeof allowInvalid === 'boolean') {
        type = type.meta({allowInvalid: allowInvalid});
      }
      if (typeof description === 'string') {
        type = type.description(description);
      }
      cfg = type.describe();
      description = cfg.description;
      if (cfg.meta) {
        if (!is.array(cfg.meta)) {
          cfg.meta = [cfg.meta];
        }
        _.each(cfg.meta, function (meta) {
          if (meta && typeof meta.allowInvalid === 'boolean') {
            allowInvalid = meta.allowInvalid;
          }
        });
      }
      construct = function (raw) {
        return validateOrThrow(raw, type, allowInvalid);
      };
    }

    this.docs.addBodyParam(
      paramName,
      description,
      toJSONSchema(paramName, is.array(type) ? type[0] : type)
    );
    this.route.action.bodyParams.push({
      extract: createBodyParamExtractor(this.rootElement, paramName, allowInvalid),
      paramName: paramName,
      construct: construct
    });
    return this;
  },
Ejemplo n.º 2
0
  bodyParam: function (paramName, attributes, Proto) {
    'use strict';
    if (Proto !== undefined) {
      // deprecated
      attributes = {
        description: attributes,
        type: Proto
      };
    }

    if (is.array(attributes.type)) {
      this.docs.addBodyParam(paramName, attributes.description, attributes.type[0].toJSONSchema(paramName));
    } else {
      this.docs.addBodyParam(paramName, attributes.description, attributes.type.toJSONSchema(paramName));
    }
    this.route.action.callback = createBodyParamBubbleWrap(
      this.route.action.callback,
      paramName,
      attributes.type,
      attributes.allowInvalid,
      this.rootElement
    );

    return this;
  },
Ejemplo n.º 3
0
  pathParam: function (paramName, attributes) {
    'use strict';
    var url = this.route.url,
      urlConstraint = url.constraint || {},
      type = attributes.type,
      required = attributes.required,
      description = attributes.description,
      constraint, regexType, cfg;

    if (attributes.isJoi) {
      type = attributes;
      required = undefined;
      description = undefined;
    }

    constraint = type;
    regexType = type;

    if (type) {
      if (typeof required === 'boolean') {
        constraint = required ? constraint.required() : constraint.optional();
      }
      if (typeof description === 'string') {
        constraint = constraint.description(description);
      }
      this.constraints.urlParams[paramName] = constraint;
      cfg = constraint.describe();
      if (is.array(cfg)) {
        cfg = cfg[0];
        type = 'string';
      } else {
        type = cfg.type;
      }
      required = Boolean(cfg.flags && cfg.flags.presence === 'required');
      description = cfg.description;
      if (
        type === 'number' &&
          _.isArray(cfg.rules) &&
          _.some(cfg.rules, function (rule) {
            return rule.name === 'integer';
          })
      ) {
        type = 'integer';
      }
      if (_.has(this.typeToRegex, type)) {
        regexType = type;
      } else {
        regexType = 'string';
      }
    }

    urlConstraint[paramName] = this.typeToRegex[regexType];
    if (!urlConstraint[paramName]) {
      throw new Error("Illegal attribute type: " + regexType);
    }
    this.route.url = internal.constructUrlObject(url.match, urlConstraint, url.methods[0]);
    this.docs.addPathParam(paramName, description, type, required);
    return this;
  },
Ejemplo n.º 4
0
isJoi = function (schema) {
  'use strict';
  if (!schema || typeof schema !== 'object' || is.array(schema)) {
    return false;
  }

  if (schema.isJoi) { // shortcut for pure joi schemas
    return true;
  }

  return Object.keys(schema).some(function (key) {
    return schema[key].isJoi;
  });
};
Ejemplo n.º 5
0
createBodyParamBubbleWrap = function (handler, paramName, Proto, allowInvalid, rootElement) {
  'use strict';
  var extractElement = elementExtractFactory(paramName, rootElement, allowInvalid),
    wrappedExtractElement = extractElement;
  if (allowInvalid) {
    wrappedExtractElement = function (req) {
      try {
        return extractElement(req);
      } catch (err) {
        return {};
      }
    };
  }
  return bubbleWrapFactory(handler, paramName, Proto, wrappedExtractElement, is.array(Proto));
};
Ejemplo n.º 6
0
createModelInstantiator = function (Model, allowInvalid) {
  'use strict';
  var multiple = is.array(Model);
  Model = multiple ? Model[0] : Model;
  var instantiate = function (raw) {
    if (!allowInvalid) {
      raw = validateOrThrow(raw, Model.prototype.schema, allowInvalid);
    }
    return new Model(raw);
  };
  if (!multiple) {
    return instantiate;
  }
  return function (raw) {
    return _.map(raw, instantiate);
  };
};
Ejemplo n.º 7
0
  queryParam: function (paramName, attributes) {
    'use strict';
    var type = attributes.type,
      required = attributes.required,
      description = attributes.description,
      allowMultiple = attributes.allowMultiple,
      constraint, cfg;

    if (attributes.isJoi) {
      type = attributes;
      required = undefined;
      description = undefined;
      allowMultiple = undefined;
    }

    constraint = type;

    if (type) {
      if (typeof required === 'boolean') {
        constraint = required ? constraint.required() : constraint.optional();
      }
      if (typeof description === 'string') {
        constraint = constraint.description(description);
      }
      if (typeof allowMultiple === 'boolean') {
        constraint = constraint.meta({allowMultiple: allowMultiple});
      }
      this.constraints.queryParams[paramName] = constraint;
      cfg = constraint.describe();
      if (is.array(cfg)) {
        cfg = cfg[0];
        type = 'string';
      } else {
        type = cfg.type;
      }
      required = Boolean(cfg.flags && cfg.flags.presence === 'required');
      description = cfg.description;
      if (cfg.meta) {
        if (!is.array(cfg.meta)) {
          cfg.meta = [cfg.meta];
        }
        _.each(cfg.meta, function (meta) {
          if (meta && typeof meta.allowMultiple === 'boolean') {
            allowMultiple = meta.allowMultiple;
          }
        });
      }
      if (
        type === 'number' &&
          _.isArray(cfg.rules) &&
          _.some(cfg.rules, function (rule) {
            return rule.name === 'integer';
          })
      ) {
        type = 'integer';
      }
    }

    this.docs.addQueryParam(
      paramName,
      description,
      type,
      required,
      Boolean(allowMultiple)
    );
    return this;
  },