|
|
@@ -4,24 +4,24 @@ import {validateProjectionSchema} from './validate-projection-schema.js';
|
|
|
/**
|
|
|
* Project data.
|
|
|
*
|
|
|
- * @param {object|Function|string} schemaOrSource
|
|
|
+ * @param {object|Function|string} schemaOrFactory
|
|
|
* @param {object} data
|
|
|
* @param {object|undefined} options
|
|
|
* @returns {*}
|
|
|
*/
|
|
|
-export function projectData(schemaOrSource, data, options = undefined) {
|
|
|
- // schemaOrSource
|
|
|
+export function projectData(schemaOrFactory, data, options = undefined) {
|
|
|
+ // schemaOrFactory
|
|
|
if (
|
|
|
- !schemaOrSource ||
|
|
|
- (typeof schemaOrSource !== 'object' &&
|
|
|
- typeof schemaOrSource !== 'function' &&
|
|
|
- typeof schemaOrSource !== 'string') ||
|
|
|
- Array.isArray(schemaOrSource)
|
|
|
+ !schemaOrFactory ||
|
|
|
+ (typeof schemaOrFactory !== 'object' &&
|
|
|
+ typeof schemaOrFactory !== 'function' &&
|
|
|
+ typeof schemaOrFactory !== 'string') ||
|
|
|
+ Array.isArray(schemaOrFactory)
|
|
|
) {
|
|
|
throw new InvalidArgumentError(
|
|
|
'Projection schema must be an Object, a Function ' +
|
|
|
'or a non-empty String, but %v was given.',
|
|
|
- schemaOrSource,
|
|
|
+ schemaOrFactory,
|
|
|
);
|
|
|
}
|
|
|
// options
|
|
|
@@ -64,35 +64,42 @@ export function projectData(schemaOrSource, data, options = undefined) {
|
|
|
const scope = (options && options.scope) || undefined;
|
|
|
// если вместо схемы передана фабрика,
|
|
|
// то извлекается фабричное значение
|
|
|
- let schema = schemaOrSource;
|
|
|
- if (typeof schemaOrSource === 'function') {
|
|
|
- schema = schemaOrSource();
|
|
|
- // если не удалось извлечь схему проекции,
|
|
|
- // то выбрасывается ошибка
|
|
|
- if (!schema || typeof schema !== 'object' || Array.isArray(schema)) {
|
|
|
+ let schemaOrName = schemaOrFactory;
|
|
|
+ if (typeof schemaOrFactory === 'function') {
|
|
|
+ schemaOrName = schemaOrFactory();
|
|
|
+ // если фабричное значение не является строкой
|
|
|
+ // или объектом, то выбрасывается ошибка
|
|
|
+ if (
|
|
|
+ !schemaOrName ||
|
|
|
+ (typeof schemaOrName !== 'object' && typeof schemaOrName !== 'string') ||
|
|
|
+ Array.isArray(schemaOrName)
|
|
|
+ ) {
|
|
|
throw new InvalidArgumentError(
|
|
|
- 'Schema factory must return an Object, but %v was given.',
|
|
|
- schema,
|
|
|
+ 'Projection schema factory must return an Object ' +
|
|
|
+ 'or a non-empty String, but %v was given.',
|
|
|
+ schemaOrName,
|
|
|
);
|
|
|
}
|
|
|
}
|
|
|
// если вместо схемы передана строка,
|
|
|
// то строка используется как название схемы
|
|
|
- else if (typeof schemaOrSource === 'string') {
|
|
|
+ let schema = schemaOrName;
|
|
|
+ if (schemaOrName && typeof schemaOrName === 'string') {
|
|
|
// если функция разрешения схемы не определена,
|
|
|
// то выбрасывается ошибка
|
|
|
if (!options || !options.resolver) {
|
|
|
throw new InvalidArgumentError(
|
|
|
- 'Unable to resolve the schema %v without a specified resolver.',
|
|
|
- schemaOrSource,
|
|
|
+ 'Unable to resolve the projection schema %v ' +
|
|
|
+ 'without a provided resolver.',
|
|
|
+ schemaOrName,
|
|
|
);
|
|
|
}
|
|
|
- schema = options.resolver(schemaOrSource);
|
|
|
+ schema = options.resolver(schemaOrName);
|
|
|
// если не удалось извлечь схему проекции,
|
|
|
// то выбрасывается ошибка
|
|
|
if (!schema || typeof schema !== 'object' || Array.isArray(schema)) {
|
|
|
throw new InvalidArgumentError(
|
|
|
- 'Schema resolver must return an Object, but %v was given.',
|
|
|
+ 'Projection schema resolver must return an Object, but %v was given.',
|
|
|
schema,
|
|
|
);
|
|
|
}
|