All files / src/modules/implementations/queries/support api-options.js

100% Statements 53/53
96.15% Branches 25/26
100% Functions 4/4
100% Lines 53/53

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 5451x 51x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 342x 7x 7x 7x 7x 7x 7x 342x 783x 783x 876x 876x 876x 876x 876x 50x 50x 147x 834x 21x 21x 21x 126x 876x 876x 876x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 783x 51x  
'use strict';
module.exports = ({ core: { publicErrors: { createError, errorCodes } }, external: { zod: { z } }, }) => {
    const nonEmptyString = z.string().min(1, { error: 'must be a non-empty string' });
    const stringList = z.array(nonEmptyString)
        .transform((values) => [...new Set(values)]);
    const accounts = stringList.default([]);
    const booleanOption = z.boolean({ error: 'must be a boolean' }).default(false);
    const dateBasis = z.enum(['posting', 'transaction'], { error: 'Invalid dateBasis' })
        .default('posting');
    const dateOption = z.iso.date({ error: 'must be a valid date in YYYY-MM-DD format' }).optional();
    const dateRange = { from: dateOption, to: dateOption };
    const usage = z.enum(['all', 'used', 'unused']).default('all');
    const valuation = z.enum(['cost', 'market'], { error: 'Invalid valuation' }).default('market');
    function validateDateRange(input, context) {
        if (input.from && input.to && input.from > input.to) {
            context.addIssue({
                code: 'custom',
                message: `date ${input.from} is after to date ${input.to}`,
                path: ['from'],
            });
        }
    }
    const invalidInput = (message) => createError(errorCodes.INVALID_API_INPUT, message, TypeError);
    function parseOptions(schema, value, operationName) {
        const input = value === undefined ? {} : value;
        const result = schema.safeParse(input);
        if (result.success)
            return result.data;
        if (typeof input !== 'object' || input === null || Array.isArray(input)) {
            throw invalidInput(`${operationName} options must be an object`);
        }
        const unrecognized = result.error.issues.find((issue) => issue.code === 'unrecognized_keys');
        if (unrecognized) {
            throw invalidInput(`Unknown ${operationName} option${unrecognized.keys.length === 1 ? '' : 's'}: ` +
                unrecognized.keys.join(', '));
        }
        const issue = result.error.issues[0];
        const name = issue.path.length > 0 ? `${issue.path.join('.')} ` : '';
        throw invalidInput(`${name}${issue.message}`);
    }
    return {
        accounts,
        booleanOption,
        dateBasis,
        dateOption,
        dateRange,
        stringList,
        usage,
        valuation,
        validateDateRange,
        parseOptions,
    };
};