Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config validation on first start #3

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 42 additions & 25 deletions lib/common/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,48 @@ const configSchema = z.object({
).transform((v) => v.toLowerCase()),
DATA_DIR: z.string().min(1).default("data"),
DB_FILE_NAME: z.string().min(1).default("denokv.sqlite3"),
}).superRefine((config, ctx) => {
if (
config.ASN_ENABLE_NAMESPACE_EXTENSION &&
(config.ASN_NAMESPACE_RANGE - 1).toString().charAt(0) === "9"
) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
ASN_NAMESPACE_RANGE: config.ASN_NAMESPACE_RANGE,
ASN_ENABLE_NAMESPACE_EXTENSION: config.ASN_ENABLE_NAMESPACE_EXTENSION,
invalidGenericNamespace: config.ASN_NAMESPACE_RANGE - 1,
},
message:
`Semantic configuration error: ASN_NAMESPACE_RANGE includes namespaces with leading 9s.\n` +
`This is not allowed when ASN_ENABLE_NAMESPACE_EXTENSION is true.`,
});
}

if (
!config.ADDITIONAL_MANAGED_NAMESPACES.every((a) =>
isValidAdditionalManagedNamespace(a.namespace, config)
)
) {
console.debug(config.ADDITIONAL_MANAGED_NAMESPACES);
ctx.addIssue({
code: z.ZodIssueCode.custom,
params: {
ASN_ENABLE_NAMESPACE_EXTENSION: config.ASN_ENABLE_NAMESPACE_EXTENSION,
ASN_NAMESPACE_RANGE: config.ASN_NAMESPACE_RANGE,
invalidAdditionalManagedNamespaces: config.ADDITIONAL_MANAGED_NAMESPACES
.filter(
(a) => !isValidAdditionalManagedNamespace(a.namespace, config),
).map((v) => `${config.ASN_PREFIX}${v.namespace}XXX - ${v.label}`),
},
message:
`Semantic configuration error: Additional managed namespaces contain invalid namespace numbers.\n` +
`The namespace numbers must have the same amount of digits as ASN_NAMESPACE_RANGE.\n` +
`If ASN_ENABLE_NAMESPACE_EXTENSION is true, the leading 9s are stripped from this calculation.\n` +
`For example, if your ASN_NAMESPACE_RANGE has two digits, instead of only XX, you can then also have 9XX, 99XX, etc.\n` +
`Note that in this case, 9X would not be valid.`,
});
}
});

/**
Expand Down Expand Up @@ -214,31 +256,6 @@ export async function validateDB(): Promise<void> {
);
}

if (
CONFIG.ASN_ENABLE_NAMESPACE_EXTENSION &&
CONFIG.ASN_NAMESPACE_RANGE.toString().charAt(0) === "9"
) {
throw new Error(
`Semantic configuration error: ASN_NAMESPACE_RANGE includes namespaces with leading 9s.\n` +
`This is not allowed when ASN_ENABLE_NAMESPACE_EXTENSION is true.`,
);
}

if (
!CONFIG.ADDITIONAL_MANAGED_NAMESPACES.every((a) =>
isValidAdditionalManagedNamespace(a.namespace)
)
) {
console.debug(CONFIG.ADDITIONAL_MANAGED_NAMESPACES);
throw new Error(
`Semantic configuration error: Additional managed namespaces contain invalid namespace numbers.\n` +
`The namespace numbers must have the same amount of digits as ASN_NAMESPACE_RANGE.\n` +
`If ASN_ENABLE_NAMESPACE_EXTENSION is true, the leading 9s are stripped from this calculation.\n` +
`For example, if your ASN_NAMESPACE_RANGE has two digits, instead of only XX, you can then also have 9XX, 99XX, etc.\n` +
`Note that in this case, 9X would not be valid.`,
);
}

if (dbConfig.ASN_BARCODE_TYPE !== CONFIG.ASN_BARCODE_TYPE) {
console.warn(
`Warning: ASN_BARCODE_TYPE has changed. This will affect the barcode generation.\n` +
Expand Down