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