Core & Sleep
Shared primitives, time variants, decimal quantities, UCUM unit subset, provenance, core.* records, and sleep.session.
@fitdata/core owns all shared primitives, envelopes, unit conversions, and the four cross-domain core.* records. It has no application or database dependency. sleep.session is composed in fitdata using the same core envelope and time primitives.
Shared primitives
Namespaced codes and extensions
- Codes match
/^[a-z][a-z0-9.-]*:[A-Za-z0-9_.-]+$/(for examplefitdata:heart_rate,fitdata:indoor_cycling,strava:calories). - Core objects use strict schemas that reject unknown properties.
- Vendor- or app-specific fields belong under
extensions, keyed by namespaced identifiers ("strava:source": { ... }).
Time variants
FitData never invents a midnight timestamp for a calendar date or a timezone for a numeric offset:
kind | Fields | Rules |
|---|---|---|
instant | at | ISO-8601 datetime with Z or explicit numeric UTC offset |
interval | start, durationSeconds | Non-negative duration, maximum 1 year (31,536,000 s) |
date | date, optional timezone | ISO calendar date (YYYY-MM-DD); optional IANA timezone verified via runtime Intl |
Quantities and decimal strings
Quantities pair value with a case-sensitive unit string. value accepts either a finite safe JSON number or a decimal string (^-?(?:0|[1-9]\d*)(?:\.\d+)?$). Decimal strings preserve exact source digits and trailing zeros, and metric range checks use exact decimal comparison (compareDecimal).
Units and conversions
convertQuantity({ value, from, to }) converts finite numbers across a reviewed UCUM-aligned subset:
| Dimension | Supported codes |
|---|---|
| Mass | kg, g, mg, ug, [lb_av], [oz_av] |
| Volume | L, mL, [foz_us], [foz_br], [cup_us], [cup_m], [tbs_us], [tsp_us], [tbs_m], [tsp_m] |
| Energy | kcal, kJ, J |
| Length | m, km, cm, mm, [mi_i], [ft_i], [in_i] |
| Time | s, min, h, d |
| Temperature | Cel, [degF], K |
Unknown units and cross-dimension conversions fail explicitly. convertQuantity never infers food density.
Provenance
Optional provenance records how an entity was produced:
Provenance kind | Required fields |
|---|---|
manual | source.provider |
sensor | source.provider |
imported | source.provider, mapper: { id, version } |
derived | source.provider, algorithm: { id, version }, inputs (references) |
source can also include externalId, device, and originalSource.
Core record kinds
core.measurement
A point-in-time observation (revision lifecycle):
- Required:
metric(namespaced code),time,value. - Optional:
laterality(left,right,bilateral). valueis a discriminated union:quantity,category(code),text,boolean, orabsent(reasoncode).
core.series
A sampled time-series stream (revision lifecycle):
- Required:
interval,channels(1–16{ metric, unit }pairs),timing,samples,resolution(sourceordownsampled). timingis either{ kind: "regular", periodSeconds }or{ kind: "irregular", offsetsSeconds }.- Every sample row width must equal
channels.length. Missing readings arenull. Irregular offsets must strictly increase and fit withininterval.durationSeconds.
core.summary
Unified rollup for a day, interval, or meal (revision lifecycle):
- Required:
scope({ kind: "day" },{ kind: "interval" }, or{ kind: "meal", label }),time. - Optional:
metrics(up to 500MetricAmountentries),totals(up to 500 qualified nutrient totals),completeness(complete,partial,unknown),statistic(sum,minimum,maximum,last,mean),weighting(sample,time,not_applicable),sampleCount,coverage,source.
core.tombstone
Logical deletion marker (revision lifecycle):
- Carries
kind: "core.tombstone",id,revision, and optionalrecordedAt/provenance. - Contains no live domain payload.
Sleep: sleep.session
Owned by fitdata (SDK) using the core revisioned envelope:
- Required:
interval,stages(array of{ stage, offsetSeconds, durationSeconds }),metrics. - Optional:
isNap(boolean). - Semantic checks guarantee sleep stages do not overlap and fit within
interval.durationSeconds. Gaps between stages remain valid and visible.