type-info.js 947 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. /**
  2. * Module dependencies
  3. */
  4. var _ = require('@sailshq/lodash');
  5. var types = require('./helpers/types');
  6. var getDisplayType = require('./get-display-type');
  7. /**
  8. * Given a type string or type schema, return an object containing more
  9. * information about it.
  10. * Useful for error messages, user interfaces, etc.
  11. *
  12. * The recognized types are identical to those of `infer()`, but instead
  13. * of returning a type schema, this function returns an object containing
  14. * more information about whatever the top-level type is.
  15. *
  16. * @param {String} type
  17. * @return {Object}
  18. */
  19. module.exports = function typeInfo(type){
  20. if (_.isArray(type)) {
  21. return _.cloneDeep(types.array);
  22. }
  23. if (_.isObject(type)) {
  24. if (getDisplayType(type) === 'dictionary') {
  25. return _.cloneDeep(types.dictionary);
  26. }
  27. }
  28. if (_.isString(type) && types[type]) {
  29. return _.cloneDeep(types[type]);
  30. }
  31. throw new Error('Unknown type: "'+type+'"');
  32. };