| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- import {InvalidArgumentError} from '@e22m4u/js-format';
- /**
- * Validate projection schema.
- *
- * @param {object|Function|string} schema
- * @param {boolean} [shallowMode]
- */
- export function validateProjectionSchema(schema, shallowMode = false) {
- // schema
- if (
- !schema ||
- (typeof schema !== 'object' &&
- typeof schema !== 'function' &&
- typeof schema !== 'string') ||
- Array.isArray(schema)
- ) {
- throw new InvalidArgumentError(
- 'Projection schema must be an Object, a Function ' +
- 'or a non-empty String, but %v was given.',
- schema,
- );
- }
- if (typeof schema !== 'object') {
- return;
- }
- // schema[k]
- Object.keys(schema).forEach(propName => {
- const propOptions = schema[propName];
- if (propOptions === undefined) {
- return;
- }
- if (
- propOptions === null ||
- (typeof propOptions !== 'object' && typeof propOptions !== 'boolean') ||
- Array.isArray(propOptions)
- ) {
- throw new InvalidArgumentError(
- 'Property options must be an Object or a Boolean, but %v was given.',
- propOptions,
- );
- }
- if (typeof propOptions === 'boolean') {
- return;
- }
- // schema[k].select
- if (
- propOptions.select !== undefined &&
- typeof propOptions.select !== 'boolean'
- ) {
- throw new InvalidArgumentError(
- 'Property option "select" must be a Boolean, but %v was given.',
- propOptions.select,
- );
- }
- // schema[k].scopes
- if (propOptions.scopes !== undefined) {
- if (
- !propOptions.scopes ||
- typeof propOptions.scopes !== 'object' ||
- Array.isArray(propOptions.scopes)
- ) {
- throw new InvalidArgumentError(
- 'Property option "scopes" must be an Object, but %v was given.',
- propOptions.scopes,
- );
- }
- Object.values(propOptions.scopes).forEach(scopeOptions => {
- if (scopeOptions === undefined) {
- return;
- }
- // schema[k].scopes[k]
- if (
- scopeOptions === null ||
- (typeof scopeOptions !== 'object' &&
- typeof scopeOptions !== 'boolean') ||
- Array.isArray(scopeOptions)
- ) {
- throw new InvalidArgumentError(
- 'Scope options must be an Object or a Boolean, but %v was given.',
- scopeOptions,
- );
- }
- // schema[k].scopes[k].select
- if (
- scopeOptions.select !== undefined &&
- typeof scopeOptions.select !== 'boolean'
- ) {
- throw new InvalidArgumentError(
- 'Scope option "select" must be a Boolean, but %v was given.',
- scopeOptions.select,
- );
- }
- });
- }
- // schema[k].schema
- if (propOptions.schema !== undefined) {
- if (
- !propOptions.schema ||
- (typeof propOptions.schema !== 'object' &&
- typeof propOptions.schema !== 'function' &&
- typeof propOptions.schema !== 'string') ||
- Array.isArray(propOptions.schema)
- ) {
- throw new InvalidArgumentError(
- 'Property option "schema" must be an Object, a Function' +
- ' or a non-empty String, but %v was given.',
- propOptions.schema,
- );
- }
- if (!shallowMode && typeof propOptions.schema === 'object') {
- validateProjectionSchema(propOptions.schema, shallowMode);
- }
- }
- });
- }
|