@htilabs/platform-common-models 0.9.0 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config/index.cjs +4 -0
- package/dist/config/index.cjs.map +1 -1
- package/dist/config/index.d.cts +16 -1
- package/dist/config/index.d.ts +16 -1
- package/dist/config/index.js +2 -0
- package/dist/config/index.js.map +1 -1
- package/dist/constants/index.cjs +473 -182
- package/dist/constants/index.cjs.map +1 -1
- package/dist/constants/index.d.cts +1 -1
- package/dist/constants/index.d.ts +1 -1
- package/dist/constants/index.js +470 -170
- package/dist/constants/index.js.map +1 -1
- package/dist/cvc-Cp4uWnAa.d.cts +298 -0
- package/dist/cvc-Cp4uWnAa.d.ts +298 -0
- package/dist/cvcs/index.cjs +21798 -811
- package/dist/cvcs/index.cjs.map +1 -1
- package/dist/cvcs/index.d.cts +25138 -2212
- package/dist/cvcs/index.d.ts +25138 -2212
- package/dist/cvcs/index.js +21736 -810
- package/dist/cvcs/index.js.map +1 -1
- package/dist/enums/index.cjs +4557 -0
- package/dist/enums/index.cjs.map +1 -0
- package/dist/enums/index.d.cts +5257 -0
- package/dist/enums/index.d.ts +5257 -0
- package/dist/enums/index.js +4354 -0
- package/dist/enums/index.js.map +1 -0
- package/dist/index.cjs +24053 -1052
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -3072
- package/dist/index.d.ts +5 -3072
- package/dist/index.js +23928 -1039
- package/dist/index.js.map +1 -1
- package/dist/mongo/index.cjs +383 -0
- package/dist/mongo/index.cjs.map +1 -0
- package/dist/mongo/index.d.cts +221 -0
- package/dist/mongo/index.d.ts +221 -0
- package/dist/mongo/index.js +351 -0
- package/dist/mongo/index.js.map +1 -0
- package/dist/{platform-auth-XVB9aHqR.d.cts → platform-auth-Dv5fu0Fh.d.cts} +22 -77
- package/dist/{platform-auth-XVB9aHqR.d.ts → platform-auth-Dv5fu0Fh.d.ts} +22 -77
- package/dist/schemas/index.cjs +30 -157
- package/dist/schemas/index.cjs.map +1 -1
- package/dist/schemas/index.js +30 -157
- package/dist/schemas/index.js.map +1 -1
- package/dist/services/index.cjs +0 -3
- package/dist/services/index.cjs.map +1 -1
- package/dist/services/index.js +0 -3
- package/dist/services/index.js.map +1 -1
- package/dist/types/index.d.cts +2 -2
- package/dist/types/index.d.ts +2 -2
- package/dist/validation/index.cjs +58 -0
- package/dist/validation/index.cjs.map +1 -1
- package/dist/validation/index.d.cts +82 -283
- package/dist/validation/index.d.ts +82 -283
- package/dist/validation/index.js +57 -0
- package/dist/validation/index.js.map +1 -1
- package/package.json +18 -1
- package/src/__sot__/README.md +91 -0
- package/src/__sot__/coreIndicatorFields.ts +331 -0
- package/src/__sot__/coreIndicators.pin.json +2149 -0
- package/src/__sot__/cvcIndex.pin.json +272 -0
- package/src/__sot__/scoringEmits.pin.json +176 -0
- package/src/__tests__/__snapshots__/cvc-sot.snap +260 -40
- package/src/__tests__/core-information-scope-invariant.test.ts +134 -0
- package/src/__tests__/cvc-136-scoring-invariant.test.ts +328 -0
- package/src/__tests__/cvc-181-182-country-parity.test.ts +163 -0
- package/src/__tests__/cvc-drift-gate.test.ts +1036 -0
- package/src/__tests__/cvc-sot-snapshot.test.ts +191 -18
- package/src/__tests__/cvc-status-parser.test.ts +150 -0
- package/src/__tests__/cvc-structural.test.ts +70 -1
- package/src/__tests__/mongo-verifier.test.ts +502 -0
- package/src/config/index.ts +8 -2
- package/src/constants/ic.ts +629 -206
- package/src/constants/service-catalog.ts +4 -2
- package/src/cvcs/activityType.v1.0.0.json +143 -0
- package/src/cvcs/adminRoles.v1.0.0.json +153 -0
- package/src/cvcs/administeringJurisdiction.v1.0.0.json +40 -40
- package/src/cvcs/ageBinReporting.v1.0.0.json +40 -40
- package/src/cvcs/assertionDomain.v1.1.0.json +647 -0
- package/src/cvcs/assertionStatus.v1.0.0.json +80 -0
- package/src/cvcs/carCorrectionReason.v1.0.0.json +89 -0
- package/src/cvcs/caseShareState.v1.0.0.json +62 -0
- package/src/cvcs/{checkInPhase.v1.0.0.json → checkInPhase.v1.1.0.json} +20 -2
- package/src/cvcs/childChangeType.v1.0.0.json +152 -0
- package/src/cvcs/citizenship.v1.0.0.json +3062 -0
- package/src/cvcs/contactInfoTypes.v1.0.0.json +40 -40
- package/src/cvcs/contactLinkType.v1.0.0.json +89 -0
- package/src/cvcs/coreDhState.v1.0.0.json +113 -0
- package/src/cvcs/coreFunctions.v1.0.0.json +746 -0
- package/src/cvcs/coreIndicators.v1.0.0.json +2918 -0
- package/src/cvcs/coreInformationScope.v1.0.0.json +1165 -0
- package/src/cvcs/coreReferralOutcome.v1.0.0.json +71 -0
- package/src/cvcs/countries.v1.0.0.json +3077 -0
- package/src/cvcs/createdBySystem.v1.0.0.json +89 -0
- package/src/cvcs/criminalOrgCategory.v1.0.0.json +116 -0
- package/src/cvcs/determinationHelperCategories.v1.0.0.json +1166 -0
- package/src/cvcs/employmentStatus.v1.0.0.json +60 -60
- package/src/cvcs/exploitationType.v1.0.0.json +106 -0
- package/src/cvcs/facilitatorEntityType.v1.0.0.json +40 -40
- package/src/cvcs/highestEducationLevelCompleted.v1.0.0.json +55 -46
- package/src/cvcs/hivStatus.v1.0.0.json +71 -0
- package/src/cvcs/hmisCurrentLivingSituation.v1.0.0.json +409 -0
- package/src/cvcs/hmisRelationshipToHoh.v1.0.0.json +104 -0
- package/src/cvcs/homelessStatus.v1.0.0.json +27 -27
- package/src/cvcs/housingSubsidyTypes.v1.0.0.json +161 -0
- package/src/cvcs/htType.v1.0.0.json +87 -0
- package/src/cvcs/identificationStatus.v1.0.0.json +18 -0
- package/src/cvcs/immigrationLegalStatusChangeType.v1.0.0.json +143 -0
- package/src/cvcs/incarcerationEpisodeType.v1.0.0.json +106 -0
- package/src/cvcs/incidentInvolvementRoles.v1.0.0.json +40 -40
- package/src/cvcs/incidentParticipationRoles.v1.0.0.json +47 -37
- package/src/cvcs/incomeBenefitsSources.v1.0.0.json +31 -2
- package/src/cvcs/index.ts +138 -1
- package/src/cvcs/juvenileJusticeEpisodeType.v1.0.0.json +28 -18
- package/src/cvcs/juvenileJusticeInvolvementStatus.v1.0.0.json +32 -32
- package/src/cvcs/livingSituation.v1.0.0.json +213 -0
- package/src/cvcs/mentalHealthStatus.v1.0.0.json +71 -0
- package/src/cvcs/militaryBranch.v1.0.0.json +20 -20
- package/src/cvcs/militaryStatus.v1.0.0.json +20 -20
- package/src/cvcs/minorAdultStatus.v1.0.0.json +80 -0
- package/src/cvcs/nameType.v1.0.0.json +40 -40
- package/src/cvcs/needsAssessmentChangeType.v1.0.0.json +134 -0
- package/src/cvcs/offenderStatus.v1.0.0.json +40 -40
- package/src/cvcs/offenderStatusFamily.v1.0.0.json +36 -36
- package/src/cvcs/personRelationships.v1.0.0.json +67 -2
- package/src/cvcs/poPetitionBasis.v1.0.0.json +96 -0
- package/src/cvcs/poStatus.v1.0.0.json +144 -0
- package/src/cvcs/poViolationStatus.v1.0.0.json +76 -0
- package/src/cvcs/programEnrollmentChangeType.v1.0.0.json +134 -0
- package/src/cvcs/provenanceSourceId.v1.0.0.json +134 -0
- package/src/cvcs/radiusBucket.v1.0.0.json +110 -0
- package/src/cvcs/recencyBand.v1.0.0.json +104 -39
- package/src/cvcs/referralEntityCategory.v1.0.0.json +40 -40
- package/src/cvcs/referralOrigin.v1.0.0.json +26 -0
- package/src/cvcs/relationshipConcern.v1.0.0.json +123 -0
- package/src/cvcs/riskCategory.v1.0.0.json +134 -0
- package/src/cvcs/riskIdentificationMilestone.v1.0.0.json +109 -0
- package/src/cvcs/riskSeverityBand.v1.0.0.json +139 -0
- package/src/cvcs/roiMethod.v1.0.0.json +107 -0
- package/src/cvcs/roiScope.v1.0.0.json +296 -0
- package/src/cvcs/roiStatus.v1.0.0.json +106 -0
- package/src/cvcs/romanticRelationshipChangeType.v1.0.0.json +162 -0
- package/src/cvcs/serviceAudience.v1.0.0.json +96 -0
- package/src/cvcs/serviceCategories.v1.0.0.json +44 -44
- package/src/cvcs/serviceDeliveryChannel.v1.0.0.json +147 -0
- package/src/cvcs/serviceMechanisms.v1.0.0.json +36 -36
- package/src/cvcs/serviceStates.v1.0.0.json +55 -33
- package/src/cvcs/sessionNoteVisibility.v1.0.0.json +80 -0
- package/src/cvcs/sessionTemplate.v1.0.0.json +283 -0
- package/src/cvcs/sexProviderSubrole.v1.0.0.json +35 -28
- package/src/cvcs/sharePurpose.v1.0.0.json +80 -12
- package/src/cvcs/smtType.v1.0.0.json +40 -40
- package/src/cvcs/sourceCategory.v1.1.0.json +8 -1
- package/src/cvcs/sourceInstrument.v1.0.0.json +458 -0
- package/src/cvcs/sourceSubtype.v1.1.0.json +36 -0
- package/src/cvcs/sourceSystem.v1.0.0.json +203 -0
- package/src/cvcs/specialNeedsDisabilities.v1.0.0.json +143 -0
- package/src/cvcs/subjectType.v1.0.0.json +80 -0
- package/src/cvcs/suggestionSource.v1.0.0.json +71 -0
- package/src/cvcs/traffickingRelevantRole.v1.0.0.json +104 -0
- package/src/cvcs/userRoles.v1.0.0.json +203 -0
- package/src/cvcs/verificationMethod.v1.0.0.json +18 -0
- package/src/cvcs/victimAbuseTypes.v1.0.0.json +276 -0
- package/src/cvcs/victimStatusFamily.v1.0.0.json +40 -40
- package/src/cvcs/whyNots.v1.0.0.json +385 -0
- package/src/generated/cvc-enums.ts +2318 -10
- package/src/mongo/catalog.ts +65 -0
- package/src/mongo/contract.ts +157 -0
- package/src/mongo/index.ts +343 -0
- package/src/mongo/similarity.ts +133 -0
- package/src/mongo/uri.ts +53 -0
- package/src/services/cvcRegistryService.ts +1 -1
- package/src/validation/cvcStatus.ts +209 -0
- package/src/validation/index.ts +1 -0
package/src/mongo/uri.ts
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @htilabs/platform-common-models — MongoDB URI parsing
|
|
3
|
+
*
|
|
4
|
+
* Home of the connection-string parsing shared by the seed guard
|
|
5
|
+
* (`scripts/lib/confirmTargetDb.ts`) and the runtime verifier
|
|
6
|
+
* (`src/mongo/index.ts`). It lives in `src/` so both can import one
|
|
7
|
+
* implementation; `scripts/` cannot be imported from `src/` at build time.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** Suffix that marks a database as a non-production scratch/test database. */
|
|
11
|
+
export const TEST_DB_SUFFIX = "-test";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Parse the target database name out of a MongoDB connection URI.
|
|
15
|
+
*
|
|
16
|
+
* The database name is the path segment after the host(s), before any query
|
|
17
|
+
* string (`?...`). Handles both `mongodb://` and `mongodb+srv://` URIs and any
|
|
18
|
+
* userinfo / multi-host / option components. Returns `null` when the URI
|
|
19
|
+
* carries no database name (e.g. `mongodb://localhost:27017` or a trailing
|
|
20
|
+
* slash with nothing after it) — callers must treat a `null` name as
|
|
21
|
+
* prod-shaped and never silently default to a production database.
|
|
22
|
+
*
|
|
23
|
+
* Implemented as manual string parsing rather than `new URL()` because Node's
|
|
24
|
+
* URL parser rejects multi-host `mongodb+srv` authorities and mishandles the
|
|
25
|
+
* comma-separated seed-list form that real connection strings use.
|
|
26
|
+
*/
|
|
27
|
+
export function parseDbName(uri: string): string | null {
|
|
28
|
+
// Strip the scheme.
|
|
29
|
+
const schemeMatch = /^mongodb(\+srv)?:\/\//i.exec(uri);
|
|
30
|
+
if (!schemeMatch) return null;
|
|
31
|
+
let rest = uri.slice(schemeMatch[0].length);
|
|
32
|
+
|
|
33
|
+
// Drop query string and fragment — the DB name precedes both.
|
|
34
|
+
const qIdx = rest.search(/[?#]/);
|
|
35
|
+
if (qIdx !== -1) rest = rest.slice(0, qIdx);
|
|
36
|
+
|
|
37
|
+
// The path begins at the first '/' after the authority. Everything before
|
|
38
|
+
// that first slash is userinfo + host(s); everything after is the DB name.
|
|
39
|
+
const slashIdx = rest.indexOf("/");
|
|
40
|
+
if (slashIdx === -1) return null;
|
|
41
|
+
|
|
42
|
+
const dbName = rest.slice(slashIdx + 1).trim();
|
|
43
|
+
return dbName.length > 0 ? dbName : null;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Classify a database name. A name is a "test" database (no confirmation
|
|
48
|
+
* required) only when it is non-null and ends in {@link TEST_DB_SUFFIX}.
|
|
49
|
+
* Everything else — including a `null` name — is prod-shaped.
|
|
50
|
+
*/
|
|
51
|
+
export function isTestDb(dbName: string | null): boolean {
|
|
52
|
+
return dbName !== null && dbName.endsWith(TEST_DB_SUFFIX);
|
|
53
|
+
}
|
|
@@ -28,7 +28,7 @@
|
|
|
28
28
|
* they can be unit-tested without a Mongoose connection.
|
|
29
29
|
*/
|
|
30
30
|
|
|
31
|
-
import { Types } from "mongoose";
|
|
31
|
+
import type { Types } from "mongoose";
|
|
32
32
|
import type { Connection, Model } from "mongoose";
|
|
33
33
|
|
|
34
34
|
import { cvcRegistryModel, cvcEntryModel } from "../models/index.js";
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @htilabs/platform-common-models — CVC `_status`/`_source` provenance parser
|
|
3
|
+
*
|
|
4
|
+
* Problem this solves: `_status` on a SOT envelope (`src/cvcs/*.json`) is
|
|
5
|
+
* unvalidated free text (115 of 179 domains carry it; the rest predate the
|
|
6
|
+
* convention). A consumer that wants to know "is this actually a draft?" has
|
|
7
|
+
* had to eyeball a string like
|
|
8
|
+
*
|
|
9
|
+
* "DRAFT — TEMPORARY: net-new value-CVC not yet in Notion; provisional
|
|
10
|
+
* 900+ numbering; codes from prototype/data/cvcs/core_accommodation_type
|
|
11
|
+
* .yaml; PENDING canonical Notion authoring (PAVE Auditability backlog)."
|
|
12
|
+
*
|
|
13
|
+
* and get it right past two real traps observed across the current
|
|
14
|
+
* population:
|
|
15
|
+
* - A leading `"CANONICAL — …"` marker is a PROVENANCE note on an
|
|
16
|
+
* ALREADY-ratified file, not a maturity warning — it must not be lumped
|
|
17
|
+
* in with `"DRAFT"`/`"PROVISIONAL"`.
|
|
18
|
+
* - `core_functions` is self-contradictory: it leads
|
|
19
|
+
* `"DRAFTED — NEEDS REVIEW"` but the same string continues
|
|
20
|
+
* `"…Canonical numbering (133), NOT provisional."` A naive
|
|
21
|
+
* leading-word reader silently picks one side; this parser flags it.
|
|
22
|
+
*
|
|
23
|
+
* This module is a pure, additive, standalone export. It does NOT touch
|
|
24
|
+
* `CvcEnvelopeSchema` (`./cvc.ts`) — `_status`/`_source` are intentionally
|
|
25
|
+
* outside that Zod schema (see its module header), and adding them there
|
|
26
|
+
* would risk changing parse behavior for every existing consumer. Callers
|
|
27
|
+
* pass the RAW parsed JSON object (before or instead of
|
|
28
|
+
* `CvcEnvelopeSchema.parse()`), not the Zod-inferred `CvcEnvelope`.
|
|
29
|
+
*
|
|
30
|
+
* Classification rule (leading token of `_status`, case-insensitive):
|
|
31
|
+
* - contains "draft" → `maturity: "draft"`
|
|
32
|
+
* - contains "provisional" → `maturity: "provisional"`
|
|
33
|
+
* - contains "canonical" → `maturity: "canonical"` (a provenance note on
|
|
34
|
+
* an already-ratified file, NOT a maturity flag — kept distinct from
|
|
35
|
+
* `"final"` below so a caller can tell "explicitly marked ratified"
|
|
36
|
+
* apart from "predates the convention entirely")
|
|
37
|
+
* - `_status` absent entirely → `maturity: "final"` (legacy/unmarked)
|
|
38
|
+
*
|
|
39
|
+
* One real domain (`share_purpose` — "Synced verbatim from canonical
|
|
40
|
+
* CVC-84…") has a present `_status` whose LEADING token ("Synced") is none
|
|
41
|
+
* of the three convention words. Rather than mis-file it as `"final"` (which
|
|
42
|
+
* per the rule above is reserved for an ABSENT field) this parser falls back
|
|
43
|
+
* to scanning the full string for a single unambiguous keyword family before
|
|
44
|
+
* giving up — see `detectMaturity` below.
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
// ─── Public shape ───────────────────────────────────────────────────────────
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The subset of a raw (pre-validation) SOT envelope this parser reads.
|
|
51
|
+
* Deliberately NOT the Zod-inferred `CvcEnvelope` from `./cvc.ts` — that type
|
|
52
|
+
* is the *parsed* shape, which does not declare `_status`/`_source` at all
|
|
53
|
+
* (they live outside the locked envelope schema on purpose). Pass the raw
|
|
54
|
+
* `JSON.parse()` result of a `src/cvcs/*.json` file here.
|
|
55
|
+
*/
|
|
56
|
+
export interface RawCvcEnvelopeProvenance {
|
|
57
|
+
_status?: unknown;
|
|
58
|
+
_source?: unknown;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The four observed maturity states. See module header for the rule. */
|
|
62
|
+
export type CvcMaturity = "draft" | "provisional" | "canonical" | "final";
|
|
63
|
+
|
|
64
|
+
export interface CvcStatusInfo {
|
|
65
|
+
/** One of the four states; see module header for the classification rule. */
|
|
66
|
+
maturity: CvcMaturity;
|
|
67
|
+
/**
|
|
68
|
+
* The text this parser classified. For a string `_status` this is that
|
|
69
|
+
* string verbatim. For the 11 domains authored with a structured
|
|
70
|
+
* `_status` object (`{ vocabStatus, authoredBy, evidence, … }` — e.g.
|
|
71
|
+
* `citizenship`, `core_functions`'s sibling `countries`), this is
|
|
72
|
+
* `_status.vocabStatus` — the field those envelopes use for exactly this
|
|
73
|
+
* purpose — not the whole object. `null` iff `_status` is absent.
|
|
74
|
+
*/
|
|
75
|
+
rawStatus: string | null;
|
|
76
|
+
/** True iff `_source` cites a `prototype/data/cvcs/*.yaml` path. */
|
|
77
|
+
sourceIsPrototype: boolean;
|
|
78
|
+
/** The cited `prototype/data/cvcs/*.yaml` path, or `null` if none. */
|
|
79
|
+
sourcePath: string | null;
|
|
80
|
+
/**
|
|
81
|
+
* True when the classification is a judgment call rather than a clean
|
|
82
|
+
* read: either the leading token disagrees with a later
|
|
83
|
+
* "NOT draft"/"NOT provisional"/"NOT canonical" qualifier in the same
|
|
84
|
+
* string (the `core_functions` shape — generic, not special-cased, so any
|
|
85
|
+
* future domain authored in the same self-contradictory shape is also
|
|
86
|
+
* caught), or the string carries more than one maturity keyword family
|
|
87
|
+
* with no leading token to arbitrate between them.
|
|
88
|
+
*/
|
|
89
|
+
ambiguous: boolean;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// ─── Implementation ─────────────────────────────────────────────────────────
|
|
93
|
+
|
|
94
|
+
const MATURITY_KEYWORDS: ReadonlyArray<{
|
|
95
|
+
keyword: string;
|
|
96
|
+
maturity: Exclude<CvcMaturity, "final">;
|
|
97
|
+
}> = [
|
|
98
|
+
{ keyword: "draft", maturity: "draft" },
|
|
99
|
+
{ keyword: "provisional", maturity: "provisional" },
|
|
100
|
+
{ keyword: "canonical", maturity: "canonical" },
|
|
101
|
+
];
|
|
102
|
+
|
|
103
|
+
/** Matches a `prototype/data/cvcs/<file>.yaml` citation anywhere in `_source`. */
|
|
104
|
+
const PROTOTYPE_SOURCE_RE = /(prototype\/data\/cvcs\/[A-Za-z0-9_.-]+\.yaml)/;
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* A maturity keyword explicitly negated ("NOT provisional.") anywhere in the
|
|
108
|
+
* string — the self-contradiction signal. Deliberately scans the WHOLE
|
|
109
|
+
* string (not just past the leading token): the qualifier that disagrees
|
|
110
|
+
* with the leading word is, by construction, never the leading word itself.
|
|
111
|
+
*/
|
|
112
|
+
const NEGATED_MATURITY_RE = /\bnot\s+(draft|provisional|canonical)\b/i;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Extract the text this parser classifies against, from either `_status`
|
|
116
|
+
* shape observed in the current SOT population:
|
|
117
|
+
* - `string` (104 of 115 status-bearing domains) → the string itself.
|
|
118
|
+
* - `{ vocabStatus: string, … }` (11 domains — a richer, hand-authored
|
|
119
|
+
* provenance record) → `vocabStatus`, the field those envelopes use for
|
|
120
|
+
* exactly this classification (values seen: `"canonical"` and
|
|
121
|
+
* `"provisional-pending-notion-ratification"`).
|
|
122
|
+
* - Anything else present but shapeless (unseen in the current 179-domain
|
|
123
|
+
* population) → `JSON.stringify` as a last resort, so a future oddity
|
|
124
|
+
* still gets a best-effort classification instead of silently vanishing.
|
|
125
|
+
* - Absent → `null`.
|
|
126
|
+
*/
|
|
127
|
+
function extractClassificationText(status: unknown): string | null {
|
|
128
|
+
if (typeof status === "string") return status;
|
|
129
|
+
if (status !== null && typeof status === "object") {
|
|
130
|
+
const vocabStatus = (status as Record<string, unknown>).vocabStatus;
|
|
131
|
+
if (typeof vocabStatus === "string") return vocabStatus;
|
|
132
|
+
return JSON.stringify(status);
|
|
133
|
+
}
|
|
134
|
+
return null;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** The first "word" of a status string, lowercased, for the leading-token rule. */
|
|
138
|
+
function leadingToken(text: string): string {
|
|
139
|
+
return (text.trim().split(/[\s—–:-]+/, 1)[0] ?? "").toLowerCase();
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Classify a present, non-null `_status` text into a maturity + ambiguity
|
|
144
|
+
* flag. Isolated from {@link parseCvcStatus} so the fallback path (the
|
|
145
|
+
* `share_purpose` shape) and the negation check are each unit-testable in
|
|
146
|
+
* isolation.
|
|
147
|
+
*/
|
|
148
|
+
function detectMaturity(text: string): { maturity: Exclude<CvcMaturity, "final">; ambiguous: boolean } {
|
|
149
|
+
const lead = leadingToken(text);
|
|
150
|
+
const leadMatch = MATURITY_KEYWORDS.find(({ keyword }) => lead.includes(keyword));
|
|
151
|
+
|
|
152
|
+
let maturity: Exclude<CvcMaturity, "final">;
|
|
153
|
+
if (leadMatch) {
|
|
154
|
+
maturity = leadMatch.maturity;
|
|
155
|
+
} else {
|
|
156
|
+
// The leading token is none of the three convention words. Observed once
|
|
157
|
+
// in the current population: `share_purpose`'s
|
|
158
|
+
// "Synced verbatim from canonical CVC-84…" — "Synced" leads, but the
|
|
159
|
+
// string is unambiguously describing an already-ratified vocabulary.
|
|
160
|
+
// Fall back to scanning the WHOLE string for maturity keyword families
|
|
161
|
+
// rather than defaulting to "final": the field IS present, and "final"
|
|
162
|
+
// per the module-header rule is reserved for an ABSENT `_status`.
|
|
163
|
+
const foundFamilies = MATURITY_KEYWORDS.filter(({ keyword }) =>
|
|
164
|
+
new RegExp(`\\b${keyword}\\w*\\b`, "i").test(text),
|
|
165
|
+
);
|
|
166
|
+
const distinctFamilies = new Set(foundFamilies.map((f) => f.maturity));
|
|
167
|
+
if (distinctFamilies.size === 1) {
|
|
168
|
+
// Single unambiguous family found in the body — safe to accept.
|
|
169
|
+
maturity = foundFamilies[0].maturity;
|
|
170
|
+
return { maturity, ambiguous: false };
|
|
171
|
+
}
|
|
172
|
+
if (distinctFamilies.size > 1) {
|
|
173
|
+
// Multiple families present with no leading token to arbitrate.
|
|
174
|
+
// Report the first-occurring family as a best-effort primary and let
|
|
175
|
+
// the caller see `ambiguous: true` — never silently pick a winner.
|
|
176
|
+
maturity = foundFamilies[0].maturity;
|
|
177
|
+
return { maturity, ambiguous: true };
|
|
178
|
+
}
|
|
179
|
+
// No maturity keyword anywhere in a present `_status` string. Unseen in
|
|
180
|
+
// the current 179-domain population; treat conservatively as
|
|
181
|
+
// "provisional" (the least-committal non-final bucket) rather than
|
|
182
|
+
// silently promoting an unrecognized status to a settled one, and flag
|
|
183
|
+
// it for human attention.
|
|
184
|
+
return { maturity: "provisional", ambiguous: true };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return { maturity, ambiguous: NEGATED_MATURITY_RE.test(text) };
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Parse a SOT envelope's `_status`/`_source` provenance fields into a
|
|
192
|
+
* structured, typed result. Pure function — no filesystem access, no
|
|
193
|
+
* dependency on `CvcEnvelopeSchema`. See module header for the full rule and
|
|
194
|
+
* the two known traps it exists to catch.
|
|
195
|
+
*/
|
|
196
|
+
export function parseCvcStatus(envelope: RawCvcEnvelopeProvenance): CvcStatusInfo {
|
|
197
|
+
const source = typeof envelope._source === "string" ? envelope._source : null;
|
|
198
|
+
const protoMatch = source?.match(PROTOTYPE_SOURCE_RE) ?? null;
|
|
199
|
+
const sourceIsPrototype = protoMatch !== null;
|
|
200
|
+
const sourcePath = protoMatch?.[1] ?? null;
|
|
201
|
+
|
|
202
|
+
const text = extractClassificationText(envelope._status);
|
|
203
|
+
if (text === null) {
|
|
204
|
+
return { maturity: "final", rawStatus: null, sourceIsPrototype, sourcePath, ambiguous: false };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
const { maturity, ambiguous } = detectMaturity(text);
|
|
208
|
+
return { maturity, rawStatus: text, sourceIsPrototype, sourcePath, ambiguous };
|
|
209
|
+
}
|
package/src/validation/index.ts
CHANGED