UNPKG

dynamodb-toolbox

Version:

Lightweight and type-safe query builder for DynamoDB and TypeScript.

80 lines (79 loc) 3.52 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.anyOfSchemaParser = anyOfSchemaParser; const index_js_1 = require("../../../errors/index.js"); const formatArrayPath_js_1 = require("../../../schema/actions/utils/formatArrayPath.js"); const cloneDeep_js_1 = require("../../../utils/cloneDeep.js"); const isObject_js_1 = require("../../../utils/validation/isObject.js"); const isString_js_1 = require("../../../utils/validation/isString.js"); const schema_js_1 = require("./schema.js"); const utils_js_1 = require("./utils.js"); function* anyOfSchemaParser(schema, inputValue, options = {}) { const { discriminator } = schema.props; const { fill = true, transform = true, valuePath } = options; let parser = undefined; let _defaultedValue = undefined; let _linkedValue = undefined; let _parsedValue = undefined; if (discriminator !== undefined && (0, isObject_js_1.isObject)(inputValue) && discriminator in inputValue) { const discriminatorValue = inputValue[discriminator]; const matchingElement = (0, isString_js_1.isString)(discriminatorValue) ? schema.match(discriminatorValue) : undefined; if (matchingElement !== undefined) { parser = (0, schema_js_1.schemaParser)(matchingElement, inputValue, options); if (fill) { _defaultedValue = parser.next().value; _linkedValue = parser.next().value; } _parsedValue = parser.next().value; } } if (parser === undefined) { for (const elementAttribute of schema.elements) { try { parser = (0, schema_js_1.schemaParser)(elementAttribute, inputValue, options); if (fill) { _defaultedValue = parser.next().value; // Note: Links cannot be used in anyOf elements or sub elements for this reason (we need the return of the yield) _linkedValue = parser.next().value; } _parsedValue = parser.next().value; break; } catch (error) { parser = undefined; _defaultedValue = undefined; _linkedValue = undefined; _parsedValue = undefined; continue; } } } if (fill) { const defaultedValue = _defaultedValue !== null && _defaultedValue !== void 0 ? _defaultedValue : (0, cloneDeep_js_1.cloneDeep)(inputValue); yield defaultedValue; const linkedValue = _linkedValue !== null && _linkedValue !== void 0 ? _linkedValue : defaultedValue; yield linkedValue; } const parsedValue = _parsedValue; if (parser === undefined || parsedValue === undefined) { const path = valuePath !== undefined ? (0, formatArrayPath_js_1.formatArrayPath)(valuePath) : undefined; throw new index_js_1.DynamoDBToolboxError('parsing.invalidAttributeInput', { message: `Attribute${path !== undefined ? ` '${path}'` : ''} does not match any of the possible sub-types.`, path, payload: { received: inputValue } }); } if (parsedValue !== undefined) { (0, utils_js_1.applyCustomValidation)(schema, parsedValue, options); } if (transform) { yield parsedValue; } else { return parsedValue; } const transformedValue = parser.next().value; return transformedValue; }