♻️ [js-core] Schema-driven configuration validation across browser-core, browser-logs, and browser-rum-core#4798
Draft
thomas-lebeau wants to merge 6 commits into
Draft
♻️ [js-core] Schema-driven configuration validation across browser-core, browser-logs, and browser-rum-core#4798thomas-lebeau wants to merge 6 commits into
thomas-lebeau wants to merge 6 commits into
Conversation
|
Bundles Sizes Evolution
|
4db0705 to
f78a2c8
Compare
Adds @datadog/js-core/configuration — a runtime-agnostic engine that validates and builds typed configuration objects from a declarative schema. Field types: string, percentage, boolean, site (Datadog domain regex), match-option (string | RegExp | function), enum (array or object form), custom (arbitrary validator). The multiple: true modifier normalizes a single value to a singleton array then validates each item. The INVALID sentinel lets validators fail the whole config rather than fall back to a default. InferredConfig<S> derives a precise TypeScript type from the schema so Configuration types can never drift from their validation. An optional display function can be passed to validateAndBuildConfiguration; each field definition may carry an errorMessage string that the engine passes to display when that field's value is invalid.
Replaces the hand-written Configuration interface and per-field validators with a declarative BROWSER_CORE_SCHEMA. Configuration is now inferred via InferredConfig<typeof BROWSER_CORE_SCHEMA> — impossible to drift. Key changes: - validateAndBuildConfiguration is now a thin wrapper: passes display.error to the schema engine so all field errorMessages surface as display.error calls - trackingConsent uses the object-form enum (INVALID on explicit invalid value) - source uses the array-form enum (defaults to 'browser' on invalid/missing) - sessionPersistence uses enum + multiple: true, normalising 'cookie' → ['cookie'] - allowedTrackingOrigins uses match-option + multiple: true - cookieOptions removed from Configuration; the three boolean flags it was derived from are now first-class schema fields - BROWSER_CORE_SCHEMA exported so browser-logs and browser-rum-core can extend it
…ation validateAndBuildConfiguration is now a pure schema call with no side effects. Two runtime checks that needed browser context move to their natural homes: - isAllowedTrackingOrigins: moved to preStartLogs/preStartRum (init layer). Signature updated to accept Configuration directly. errorStack parameter removed from validateAndBuild* — it was only threaded through to reach isAllowedTrackingOrigins. - buildCookieOptions: moved out of validateAndBuildConfiguration return value. sessionInCookie.ts now calls buildCookieOptions(configuration) directly. Signature updated to accept the three boolean fields from Configuration. - sessionStore: normalizePersistenceList simplified — sessionPersistence is always SessionPersistence[] | undefined after schema validation.
Extends BROWSER_CORE_SCHEMA with three logs-specific fields:
- forwardErrorsToLogs: boolean, default true
- forwardConsoleLogs: custom validator (undefined → [], 'all' → all values,
valid array → deduped, invalid → INVALID)
- forwardReports: same pattern as forwardConsoleLogs
LogsConfiguration is now InferredConfig<typeof LOGS_SCHEMA> & { requestErrorResponseLengthLimit }.
validateAndBuildLogsConfiguration reduces to: schema call + constant field.
Extends BROWSER_CORE_SCHEMA with ~20 RUM-specific fields covering sample rates, booleans, enums (defaultPrivacyLevel, traceContextInjection), arrays (excludedActivityUrls, plugins, trackFeatureFlagsForEvents), and passthroughs. RumConfiguration is InferredConfig<typeof RUM_SCHEMA> with four additions that require post-schema computation: - startSessionReplayRecordingManually: conditional default on sessionReplaySampleRate - rulePsr: derived from traceSampleRate - allowedTracingUrls / trackResourceHeaders / allowedGraphQlUrls: complex normalization helpers stay unchanged errorStack removed from validateAndBuildRumConfiguration signature — it was only threaded through to isAllowedTrackingOrigins, which now lives in the boot layer.
f78a2c8 to
8341a2a
Compare
- Replace CustomField/INVALID sentinel with UnionField, SchemaField, and FunctionField types for cleaner type-safe field definitions - Replace per-field errorMessage with auto-generated error messages and a strict: false flag for backward-compatible validation fallback - Extract buildCookieOptions/CookieConfiguration from browser-core configuration.ts into cookie.ts - Remove validateAndBuildConfiguration, isSampleRate, and buildCookieOptions from browser-core (now in js-core or cookie.ts) - Update browser-core, browser-logs, and browser-rum-core schemas to use the new field types (union, schema, function)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Configuration validation in
browser-core,browser-logs, andbrowser-rum-corewas previously done imperatively — each package had its own ad-hoc validation logic with repetitive checks, inconsistent error handling, and no shared abstraction.This PR introduces a schema-driven configuration system in
@datadog/js-corethat declaratively describes fields, their types, defaults, and validation rules. Browser packages then derive theirConfigurationtypes and validation functions from these schemas.Changes
@datadog/js-core: Addconfigurationentry point withvalidateAndBuildConfiguration,InferredConfig, and field definition types (StringField,BooleanField,PercentageField,SiteField,MatchOptionField,EnumField,CustomField). Includes anINVALIDsentinel value that custom validators can return to fail the whole configuration.browser-core: Replace imperativevalidateAndBuildConfigurationand manualConfigurationinterface withBROWSER_CORE_SCHEMA+InferredConfig<typeof BROWSER_CORE_SCHEMA>. Cookie-option fields (useSecureSessionCookie, etc.) are moved into the schema.buildCookieOptionsnow takes a typedCookieConfigurationrather than raw init config.browser-logs: ReplaceLogsConfigurationinterface and imperative validation withLOGS_SCHEMAthat spreadsBROWSER_CORE_SCHEMAand addsforwardErrorsToLogs,forwardConsoleLogs,forwardReports, andrequestErrorResponseLengthLimit.browser-rum-core: Same pattern —RUM_SCHEMAspreadsBROWSER_CORE_SCHEMAand adds all RUM-specific fields.Test instructions
Checklist