From b6c309af2e48469e7fba657e201e7ae9ddd8ffa8 Mon Sep 17 00:00:00 2001 From: Emmanuel Raviart Date: Sat, 20 Aug 2022 17:56:50 +0200 Subject: [PATCH] Add forgotten module. --- src/lib/auditors/legal.ts | 99 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/lib/auditors/legal.ts diff --git a/src/lib/auditors/legal.ts b/src/lib/auditors/legal.ts new file mode 100644 index 0000000..34de0d0 --- /dev/null +++ b/src/lib/auditors/legal.ts @@ -0,0 +1,99 @@ +import { + type Audit, + auditChain, + auditRequire, + auditString, + auditTest, + auditOptions, +} from "@auditors/core" + +export const auditId = auditChain( + auditString, + auditTest((id) => id.length === 20, "Invalid length for ID"), +) + +export function auditVersions( + audit: Audit, + dataUnknown: unknown, +): [unknown, unknown] { + if (dataUnknown == null) { + return [dataUnknown, null] + } + if (typeof dataUnknown !== "object") { + return audit.unexpectedType(dataUnknown, "object") + } + + const data = { ...dataUnknown } + const errors: { [key: string]: unknown } = {} + const remainingKeys = new Set(Object.keys(data)) + + audit.attribute( + data, + "VERSION", + true, + errors, + remainingKeys, + auditVersionsVersion, + auditRequire, + ) + + return audit.reduceRemaining(data, errors, remainingKeys) +} + +export function auditVersionsVersion( + audit: Audit, + dataUnknown: unknown, +): [unknown, unknown] { + if (dataUnknown == null) { + return [dataUnknown, null] + } + if (typeof dataUnknown !== "object") { + return audit.unexpectedType(dataUnknown, "object") + } + + const data = { ...dataUnknown } + const errors: { [key: string]: unknown } = {} + const remainingKeys = new Set(Object.keys(data)) + + audit.attribute( + data, + "@id", + true, + errors, + remainingKeys, + auditId, + auditRequire, + ) + audit.attribute( + data, + "@fin", + true, + errors, + remainingKeys, + auditString, + auditOptions(["2999-01-01"]), + auditRequire, + ) + audit.attribute( + data, + "@etat", + true, + errors, + remainingKeys, + auditString, + auditOptions([""]), + auditRequire, + ) + audit.attribute( + data, + "@debut", + true, + errors, + remainingKeys, + auditString, + auditOptions(["2999-01-01"]), + auditRequire, + ) + + return audit.reduceRemaining(data, errors, remainingKeys) +}