UNPKG

@nestjs/config

Version:

Nest - modern, fast, powerful node.js web framework (@config)

50 lines (49 loc) • 1.64 kB
/** * Joi defaults to `allowUnknown: false` and `abortEarly: true`, which would * reject the unrelated variables that are always present in `process.env` and * report only the first failure. Previous versions of this package overrode * both, so they remain the defaults for Joi schemas unless the user opts out. */ const JOI_DEFAULT_LIBRARY_OPTIONS = { abortEarly: false, allowUnknown: true, }; /** * Validates config using a Standard Schema (e.g. Zod, Arktype). * @see https://standardschema.dev/ */ export async function validateWithStandardSchema(schema, config, options) { const result = await schema['~standard'].validate(config, getLibraryOptions(schema, options)); // A falsy "issues" value indicates success, see https://standardschema.dev/ if (result.issues) { return { error: new Error(formatIssues(result.issues)), value: config }; } return { value: result.value }; } function getLibraryOptions(schema, options) { if (schema['~standard'].vendor !== 'joi') { return options; } return { ...options, libraryOptions: { ...JOI_DEFAULT_LIBRARY_OPTIONS, ...options?.libraryOptions, }, }; } function formatIssues(issues) { return issues .map(issue => { const path = formatIssuePath(issue.path); return path ? `${path}: ${issue.message}` : issue.message; }) .join('\n'); } function formatIssuePath(path) { return (path ?? []) .map(segment => typeof segment === 'object' && segment !== null ? String(segment.key) : String(segment)) .join('.'); }