validate-projection-schema.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import {InvalidArgumentError} from '@e22m4u/js-format';
  2. /**
  3. * Validate projection schema.
  4. *
  5. * @param {object} schema
  6. * @param {boolean} shallowMode
  7. * @returns {undefined}
  8. */
  9. export function validateProjectionSchema(schema, shallowMode = false) {
  10. // schema
  11. if (!schema || typeof schema !== 'object' || Array.isArray(schema)) {
  12. throw new InvalidArgumentError(
  13. 'Projection schema must be an Object, but %v was given.',
  14. schema,
  15. );
  16. }
  17. // shallowMode
  18. if (typeof shallowMode !== 'boolean') {
  19. throw new InvalidArgumentError(
  20. 'Parameter "shallowMode" must be a Boolean, but %v was given.',
  21. shallowMode,
  22. );
  23. }
  24. Object.keys(schema).forEach(propName => {
  25. // schema[k]
  26. const options = schema[propName];
  27. if (options === undefined) {
  28. return;
  29. }
  30. if (
  31. options === null ||
  32. (typeof options !== 'boolean' && typeof options !== 'object') ||
  33. Array.isArray(options)
  34. ) {
  35. throw new InvalidArgumentError(
  36. 'Property options must be a Boolean or an Object, but %v was given.',
  37. options,
  38. );
  39. }
  40. if (typeof options === 'boolean') {
  41. return;
  42. }
  43. // schema[k].select
  44. if (options.select !== undefined && typeof options.select !== 'boolean') {
  45. throw new InvalidArgumentError(
  46. 'Property option "select" must be a Boolean, but %v was given.',
  47. options.select,
  48. );
  49. }
  50. // schema[k].schema
  51. if (options.schema !== undefined) {
  52. if (
  53. !options.schema ||
  54. (typeof options.schema !== 'object' &&
  55. typeof options.schema !== 'function') ||
  56. Array.isArray(options.schema)
  57. ) {
  58. throw new InvalidArgumentError(
  59. 'Embedded schema must be an Object or a Function ' +
  60. 'that returns a schema, but %v was given.',
  61. options.schema,
  62. );
  63. }
  64. if (!shallowMode && typeof options.schema === 'object') {
  65. validateProjectionSchema(options.schema, shallowMode);
  66. }
  67. }
  68. // schema[k].scopes
  69. if (options.scopes !== undefined) {
  70. if (
  71. !options.scopes ||
  72. typeof options.scopes !== 'object' ||
  73. Array.isArray(options.scopes)
  74. ) {
  75. throw new InvalidArgumentError(
  76. 'Property option "scopes" must be an Object, but %v was given.',
  77. options.scopes,
  78. );
  79. }
  80. Object.keys(options.scopes).forEach(scopeName => {
  81. // schema[k].scopes[k]
  82. const scopeOptions = options.scopes[scopeName];
  83. if (scopeOptions === undefined) {
  84. return;
  85. }
  86. if (
  87. scopeOptions === null ||
  88. (typeof scopeOptions !== 'boolean' &&
  89. typeof scopeOptions !== 'object') ||
  90. Array.isArray(scopeOptions)
  91. ) {
  92. throw new InvalidArgumentError(
  93. 'Scope options must be a Boolean or an Object, but %v was given.',
  94. scopeOptions,
  95. );
  96. }
  97. if (typeof scopeOptions === 'boolean') {
  98. return;
  99. }
  100. // schema[k].scopes[k].select
  101. if (scopeOptions.select !== undefined) {
  102. if (typeof scopeOptions.select !== 'boolean') {
  103. throw new InvalidArgumentError(
  104. 'Scope option "select" must be a Boolean, but %v was given.',
  105. scopeOptions.select,
  106. );
  107. }
  108. }
  109. });
  110. }
  111. });
  112. }