get-default-exemplar.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /**
  2. * Module dependencies
  3. */
  4. var _ = require('@sailshq/lodash');
  5. var typeInfo = require('./type-info');
  6. var dehydrate = require('./dehydrate');
  7. var buildSchemaIterator = require('./helpers/build-schema-iterator');
  8. /**
  9. * Given a type schema, return an exemplar which accepts precisely the same set of values.
  10. *
  11. * @param {*} typeSchema
  12. * @return {*}
  13. */
  14. module.exports = function getDefaultExemplar (typeSchema) {
  15. // Dehydrate the type schema to avoid circular recursion
  16. var dehydratedTypeSchema = dehydrate(typeSchema);
  17. // Configure type schema iterator
  18. return buildSchemaIterator(
  19. function onFacetDict(facetDictionary, parentKeyOrIndex, callRecursive){
  20. return _.reduce(facetDictionary, function (memo, val, key) {
  21. var facet = callRecursive(val, key);
  22. memo[key] = facet;
  23. return memo;
  24. }, {});
  25. },
  26. function onPatternArray(patternArray, parentKeyOrIndex, iterateRecursive){
  27. var pattern = iterateRecursive(patternArray[0], 0);
  28. return [ pattern ];
  29. },
  30. function onGenericDict(schema, parentKeyOrIndex){
  31. return typeInfo('dictionary').getExemplar();
  32. },
  33. function onGenericArray(schema, parentKeyOrIndex){
  34. return typeInfo('array').getExemplar();
  35. },
  36. function onOther(schema, parentKeyOrIndex){
  37. return typeInfo(schema).getExemplar();
  38. }
  39. )(dehydratedTypeSchema);
  40. };