dynamodb-toolbox
Version:
Lightweight and type-safe query builder for DynamoDB and TypeScript.
62 lines (61 loc) • 2.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isRequired = exports.requiringOptions = void 0;
exports.schemaFormatter = schemaFormatter;
const index_js_1 = require("../../../errors/index.js");
const formatArrayPath_js_1 = require("../../../schema/actions/utils/formatArrayPath.js");
const any_js_1 = require("./any.js");
const anyOf_js_1 = require("./anyOf.js");
const list_js_1 = require("./list.js");
const map_js_1 = require("./map.js");
const primitive_js_1 = require("./primitive.js");
const record_js_1 = require("./record.js");
const set_js_1 = require("./set.js");
exports.requiringOptions = new Set(['always', 'atLeastOnce']);
const isRequired = ({ props }) => { var _a; return exports.requiringOptions.has((_a = props.required) !== null && _a !== void 0 ? _a : 'atLeastOnce'); };
exports.isRequired = isRequired;
function* schemaFormatter(schema, rawValue, options = {}) {
const { format = true, transform = true, valuePath } = options;
if (rawValue === undefined) {
if ((0, exports.isRequired)(schema) && options.partial !== true) {
const path = valuePath !== undefined ? (0, formatArrayPath_js_1.formatArrayPath)(valuePath) : undefined;
throw new index_js_1.DynamoDBToolboxError('formatter.missingAttribute', {
message: `Missing required attribute for formatting${path !== undefined ? `: '${path}'` : ''}.`,
path,
payload: {}
});
}
if (transform) {
if (format) {
yield undefined;
}
else {
return undefined;
}
}
return undefined;
}
switch (schema.type) {
case 'any':
return yield* (0, any_js_1.anySchemaFormatter)(schema, rawValue, options);
case 'null':
case 'boolean':
case 'number':
case 'string':
case 'binary':
return yield* (0, primitive_js_1.primitiveSchemaFormatter)(schema, rawValue, {
...options,
attributes: undefined
});
case 'set':
return yield* (0, set_js_1.setSchemaFormatter)(schema, rawValue, { ...options, attributes: undefined });
case 'list':
return yield* (0, list_js_1.listSchemaFormatter)(schema, rawValue, options);
case 'map':
return yield* (0, map_js_1.mapSchemaFormatter)(schema, rawValue, options);
case 'record':
return yield* (0, record_js_1.recordSchemaFormatter)(schema, rawValue, options);
case 'anyOf':
return yield* (0, anyOf_js_1.anyOfSchemaFormatter)(schema, rawValue, options);
}
}