is-specific.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /**
  2. * Module dependencies
  3. */
  4. var _ = require('@sailshq/lodash');
  5. var buildSchemaIterator = require('./helpers/build-schema-iterator');
  6. /**
  7. * Return whether or not this schema is "specific"-- meaning
  8. * its type is either "string", "number", "boolean", "lamda", a
  9. * faceted dictionary, or a patterned array.
  10. *
  11. * This check is not recursive by default (i.e. `foo: { bar: [], baz: {} }`
  12. * would be considered "specific"). Set `checkRecursively` to true to enable
  13. * recursive parsing.
  14. *
  15. * @param {*} schema
  16. * @param {Boolean} checkRecursively [defaults to false]
  17. * @param {Boolean} isExemplar [defaults to false]
  18. * if set, the schema will be treated as an exemplars (rather than a type schema)
  19. *
  20. * @return {Boolean}
  21. */
  22. module.exports = function isSpecific (schema, checkRecursively, isExemplar) {
  23. // Build iterator
  24. var iterator = (buildSchemaIterator(
  25. function onFacetDict(facetDictionary, parentKeyOrIndex, callRecursive){
  26. if (!checkRecursively) {
  27. return true;
  28. }
  29. return _.reduce(facetDictionary, function (isSpecific, val, key) {
  30. return isSpecific && callRecursive(val, key);
  31. }, true);
  32. },
  33. function onPatternArray(patternArray, parentKeyOrIndex, iterateRecursive){
  34. if (!checkRecursively) {
  35. return true;
  36. }
  37. return iterateRecursive(patternArray[0], 0);
  38. },
  39. function onGenericDict(schema, parentKeyOrIndex){
  40. return false;
  41. },
  42. function onGenericArray(schema, parentKeyOrIndex){
  43. return false;
  44. },
  45. function onOther(schema, parentKeyOrIndex){
  46. if (isExemplar) {
  47. return (schema !== '*' && schema !== '===');
  48. }
  49. return (schema !== 'json' && schema !== 'ref');
  50. }
  51. ));
  52. return iterator(schema);
  53. };