validate-projection-schema.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. typeof options.schema !== 'string' &&
  57. typeof options.schema !== 'symbol') ||
  58. Array.isArray(options.schema)
  59. ) {
  60. throw new InvalidArgumentError(
  61. 'Embedded schema must be an Object, a Function, ' +
  62. 'a non-empty String or a Symbol, but %v was given.',
  63. options.schema,
  64. );
  65. }
  66. if (!shallowMode && typeof options.schema === 'object') {
  67. validateProjectionSchema(options.schema, shallowMode);
  68. }
  69. }
  70. // schema[k].scopes
  71. if (options.scopes !== undefined) {
  72. if (
  73. !options.scopes ||
  74. typeof options.scopes !== 'object' ||
  75. Array.isArray(options.scopes)
  76. ) {
  77. throw new InvalidArgumentError(
  78. 'Property option "scopes" must be an Object, but %v was given.',
  79. options.scopes,
  80. );
  81. }
  82. Object.keys(options.scopes).forEach(scopeName => {
  83. // schema[k].scopes[k]
  84. const scopeOptions = options.scopes[scopeName];
  85. if (scopeOptions === undefined) {
  86. return;
  87. }
  88. if (
  89. scopeOptions === null ||
  90. (typeof scopeOptions !== 'boolean' &&
  91. typeof scopeOptions !== 'object') ||
  92. Array.isArray(scopeOptions)
  93. ) {
  94. throw new InvalidArgumentError(
  95. 'Scope options must be a Boolean or an Object, but %v was given.',
  96. scopeOptions,
  97. );
  98. }
  99. if (typeof scopeOptions === 'boolean') {
  100. return;
  101. }
  102. // schema[k].scopes[k].select
  103. if (scopeOptions.select !== undefined) {
  104. if (typeof scopeOptions.select !== 'boolean') {
  105. throw new InvalidArgumentError(
  106. 'Scope option "select" must be a Boolean, but %v was given.',
  107. scopeOptions.select,
  108. );
  109. }
  110. }
  111. });
  112. }
  113. });
  114. }