diff --git a/.changeset/39173-field-precision-zero-refused.md b/.changeset/39173-field-precision-zero-refused.md new file mode 100644 index 00000000000..8c4947d394b --- /dev/null +++ b/.changeset/39173-field-precision-zero-refused.md @@ -0,0 +1,21 @@ +--- +'@objectstack/spec': minor +--- + +fix(spec)!: a field declaring `precision: 0` is refused when it is saved or published, with a sentence that sends decimal places to `scale` + +Clause-②: no (narrowing) +Metadata-protocol: narrowed +Skills-docs: follow-up #39213 + + + +**BREAKING** accept-set narrowing on the field surface (`@objectstack/spec`). + +`precision` is a field's total digit count, the `p` of a DECIMAL(p, s), and every write is checked against it. So a field declaring `precision: 0` could store only the value 0: it saved and published with no complaint, and then every other value was refused ("must have at most 0 digits in total"). The author who writes it almost always means "no decimal places", which is `scale: 0`. + +- **What is refused.** `precision: 0` (and any value below 1) on any field, at `os validate`, `defineStack`, the metadata save door (`422 INVALID_METADATA`, located at `fields.FIELD.precision`) and the publish of an object draft. The refusal reads: "`precision` must be a positive integer: it is the total digit count of the value … Decimal places are not declared with `precision`: on a `number`, `percent`, `rating` or `slider` field they are `scale` (a whole number declares `scale: 0`, not `precision: 0`); on a `currency` field they are the currency's own and `scale` is refused there, so delete `precision`. …" +- **FROM → TO.** `{ type: 'number', precision: 0 }` → `{ type: 'number', scale: 0 }` for a whole number (on a `currency` field, no `precision` at all); a real digit bound declares the total digits, such as `precision: 10, scale: 2` (up to 99999999.99); no limit at all omits `precision`. +- **The one-line fix.** On a `number`, `percent`, `rating` or `slider` field, replace `precision: 0` with `scale: 0`; on a `currency` field (where `scale` is refused and the currency decides the decimal places), delete `precision`. +- **Stored objects.** Nothing is rewritten: dropping the key would make every value writable, and whether the author meant `scale: 0` or a real bound is theirs to say. An object already active with `precision: 0` keeps loading and keeps refusing non-zero values until it is next saved: the engine registers it anyway with a `metadata_spec_invalid` error line naming the object, and metadata reads mark it in `_diagnostics`. That next save, and the publish of a draft stored before this release, is refused until the field is fixed. `os migrate meta` lists the `field-precision-zero-refused` entry. +- **Unchanged.** A `precision` of 1 or more parses and is enforced exactly as before. diff --git a/content/docs/references/data/field.mdx b/content/docs/references/data/field.mdx index 349cf06f66b..a361d80715f 100644 --- a/content/docs/references/data/field.mdx +++ b/content/docs/references/data/field.mdx @@ -65,7 +65,7 @@ const result = CurrencyConfigSchema.parse(data); | **minLength** | `integer` | optional | Min character length (positive integer; `minLength: 0` is refused — express "no minimum" by omitting the key). Only authorable on types that store a bounded string: text, textarea, email, url, phone, password, markdown, html, richtext, code, signature, qrcode. Checked on the WRITTEN value only (the `min`/`max` transition-gate class): a stored value shorter than a bound declared later is never re-read and survives unrelated edits — only a write carrying a too-short value is refused. | | **valueDomain** | `Enum<'iana_time_zone' \| 'iso_4217_currency' \| 'iso_3166_alpha2'>` | optional | Standard value domain the WRITTEN value must be a member of: `iana_time_zone` (an IANA/tzdb zone identifier such as `UTC`, `Asia/Kolkata`, `Europe/Kyiv` — membership is the `Intl.DateTimeFormat` probe, never the `Intl.supportedValuesOf` enumeration, which omits `UTC`), `iso_4217_currency` (an ISO 4217 alphabetic currency code, uppercase, e.g. `CHF`) or `iso_3166_alpha2` (an ISO 3166-1 alpha-2 country code, uppercase, e.g. `CH`). The same closed vocabulary and the same membership predicate as a settings specifier's `valueDomain`. Only authorable on `text` — the one type whose stored value is a single plain string naming the member. Checked on the WRITTEN value only (the `min`/`max`/`maxLength` transition-gate class): a stored value outside a domain declared later is never re-read and survives unrelated edits — only a write carrying a non-member is refused, with the field error code `value_domain`. Reach for it precisely where a pattern cannot help: `^[A-Z]{2}$` admits `ZZ`, and `Mars/Olympus` is a shape-valid zone that does not exist. | | **rows** | `integer` | optional | Height of the INLINE multiline editor, in text rows (positive integer — the HTML textarea `rows` attribute; fullscreen/dialog editor surfaces size themselves and ignore it). Only authorable on multiline editor types: textarea, markdown, html, richtext. Omit it for the widget default height. | -| **precision** | `integer` | optional | Total digits (non-negative integer) — the `p` of a DECIMAL(p, s): the digits of the value, integer and fraction together, counted at the field's decimal places, so `precision: 5, scale: 2` holds up to 999.99 and refuses 1234.5 (1234.50 is 6 digits). Enforced on writes of `number`, `currency`, `percent`, `rating` and `slider` fields: a value that needs more digits is refused with field code `max_precision`, never rounded. Counted on the STORED value: at the declared `scale` when one applies, else at the value's own decimal places (leading zeros never count) — so on a `currency` field, where `scale` is refused, an amount's written decimals count toward the total; a fraction-stored `percent` is counted two places further right (`scale + 2`, or 2 with no `scale`), which makes the count that of the percentage-point value as displayed. Not decimal places (that is `scale`; a currency's are its CLDR fraction digits) and not a column size: every numeric column keeps the platform's fixed exact decimal whatever this declares. Not read on any other field type. | +| **precision** | `integer` | optional | Total digits (positive integer; `precision: 0` is refused — it would admit only the value 0, and zero decimal places is `scale: 0`; omit the key for no digit limit) — the `p` of a DECIMAL(p, s): the digits of the value, integer and fraction together, counted at the field's decimal places, so `precision: 5, scale: 2` holds up to 999.99 and refuses 1234.5 (1234.50 is 6 digits). Enforced on writes of `number`, `currency`, `percent`, `rating` and `slider` fields: a value that needs more digits is refused with field code `max_precision`, never rounded. Counted on the STORED value: at the declared `scale` when one applies, else at the value's own decimal places (leading zeros never count) — so on a `currency` field, where `scale` is refused, an amount's written decimals count toward the total; a fraction-stored `percent` is counted two places further right (`scale + 2`, or 2 with no `scale`), which makes the count that of the percentage-point value as displayed. Not decimal places (that is `scale`; a currency's are its CLDR fraction digits) and not a column size: every numeric column keeps the platform's fixed exact decimal whatever this declares. Not read on any other field type. | | **scale** | `integer` | optional | Decimal places (integer 0-100). Applies to `number`, `percent`, `rating` and `slider` fields, where it is enforced on writes, and to a `formula` field, whose computed result is rounded to it — a `formula` whose `returnType` is `percent` counts the places of its displayed percentage, as a `percent` field does, so a fraction result is rounded to `scale + 2` places. REFUSED on a `currency` field — delete it there: a currency amount's decimal places are its currency's, so the currency's CLDR fraction digits decide how the amount displays, and a currency write's decimal places stay unconstrained. OMITTED on a `percent` field ⇒ 0 decimal places, so a stored 0.25 reads `25%` on every face; omitted on any OTHER numeric type declares NO fixed width — the value keeps its natural precision, and a DECLARED `scale: 0` (a year, a fiscal period, an ordinal) stays distinguishable from having declared nothing, so nothing is defaulted there. Consumers resolve the effective width by calling `resolveFieldScale` from `@objectstack/spec/data`, the single source for an absent `scale`: a renderer that spells its own fallback is a width no other face can see, and that is how one stored 0.25 came to read `25%` on the read-only cell and `25.00%` in the edit widget. On a `percent` field this is the number of decimal places of the PERCENTAGE-POINT value as displayed and entered — `scale: 2` means 12.34% — and the STORED precision derives from the field's storage scale rather than being declared again: a fraction-stored percent (no `max`, or a `max` at or below 1) stores 12.34% as 0.1234 and is allowed `scale + 2` decimal places at the write seam, while a whole-percent field (`max` above 1) stores the displayed number itself and is allowed exactly `scale`. `number`, `rating` and `slider` are allowed exactly `scale`. The upper bound is the platform's, not a policy: renderers turn `scale` into fraction digits through `toFixed` and `Intl.NumberFormat`'s `maximumFractionDigits`, both of which throw a RangeError above 100 — so a larger declaration is unrenderable by any conforming consumer. | | **min** | `number` | optional | Minimum value. Checked on the WRITTEN value only — the same transition-gate class as `requiredWhen`: an UPDATE validates just the fields the payload carries, so a stored value below a bound declared later is never re-read and survives unrelated edits; only a write that carries an out-of-bound value is refused, and a repairing write is accepted. For an invariant re-checked on every write, declare a `validations[]` `script` rule instead. | | **max** | `number` | optional | Maximum value. Checked on the WRITTEN value only — the same transition-gate class as `min`: a stored value above a bound declared later is never re-read and survives unrelated edits; only a write that carries an out-of-bound value is refused. For an invariant re-checked on every write, declare a `validations[]` `script` rule instead. | diff --git a/content/docs/references/data/object.mdx b/content/docs/references/data/object.mdx index 7c60c56deea..dafff42e948 100644 --- a/content/docs/references/data/object.mdx +++ b/content/docs/references/data/object.mdx @@ -258,7 +258,7 @@ const result = ApiMethod.parse(data); | **minLength** | `integer` | optional | Min character length (positive integer; `minLength: 0` is refused — express "no minimum" by omitting the key). Only authorable on types that store a bounded string: text, textarea, email, url, phone, password, markdown, html, richtext, code, signature, qrcode. Checked on the WRITTEN value only (the `min`/`max` transition-gate class): a stored value shorter than a bound declared later is never re-read and survives unrelated edits — only a write carrying a too-short value is refused. | | **valueDomain** | `Enum<'iana_time_zone' \| 'iso_4217_currency' \| 'iso_3166_alpha2'>` | optional | Standard value domain the WRITTEN value must be a member of: `iana_time_zone` (an IANA/tzdb zone identifier such as `UTC`, `Asia/Kolkata`, `Europe/Kyiv` — membership is the `Intl.DateTimeFormat` probe, never the `Intl.supportedValuesOf` enumeration, which omits `UTC`), `iso_4217_currency` (an ISO 4217 alphabetic currency code, uppercase, e.g. `CHF`) or `iso_3166_alpha2` (an ISO 3166-1 alpha-2 country code, uppercase, e.g. `CH`). The same closed vocabulary and the same membership predicate as a settings specifier's `valueDomain`. Only authorable on `text` — the one type whose stored value is a single plain string naming the member. Checked on the WRITTEN value only (the `min`/`max`/`maxLength` transition-gate class): a stored value outside a domain declared later is never re-read and survives unrelated edits — only a write carrying a non-member is refused, with the field error code `value_domain`. Reach for it precisely where a pattern cannot help: `^[A-Z]{2}$` admits `ZZ`, and `Mars/Olympus` is a shape-valid zone that does not exist. | | **rows** | `integer` | optional | Height of the INLINE multiline editor, in text rows (positive integer — the HTML textarea `rows` attribute; fullscreen/dialog editor surfaces size themselves and ignore it). Only authorable on multiline editor types: textarea, markdown, html, richtext. Omit it for the widget default height. | -| **precision** | `integer` | optional | Total digits (non-negative integer) — the `p` of a DECIMAL(p, s): the digits of the value, integer and fraction together, counted at the field's decimal places, so `precision: 5, scale: 2` holds up to 999.99 and refuses 1234.5 (1234.50 is 6 digits). Enforced on writes of `number`, `currency`, `percent`, `rating` and `slider` fields: a value that needs more digits is refused with field code `max_precision`, never rounded. Counted on the STORED value: at the declared `scale` when one applies, else at the value's own decimal places (leading zeros never count) — so on a `currency` field, where `scale` is refused, an amount's written decimals count toward the total; a fraction-stored `percent` is counted two places further right (`scale + 2`, or 2 with no `scale`), which makes the count that of the percentage-point value as displayed. Not decimal places (that is `scale`; a currency's are its CLDR fraction digits) and not a column size: every numeric column keeps the platform's fixed exact decimal whatever this declares. Not read on any other field type. | +| **precision** | `integer` | optional | Total digits (positive integer; `precision: 0` is refused — it would admit only the value 0, and zero decimal places is `scale: 0`; omit the key for no digit limit) — the `p` of a DECIMAL(p, s): the digits of the value, integer and fraction together, counted at the field's decimal places, so `precision: 5, scale: 2` holds up to 999.99 and refuses 1234.5 (1234.50 is 6 digits). Enforced on writes of `number`, `currency`, `percent`, `rating` and `slider` fields: a value that needs more digits is refused with field code `max_precision`, never rounded. Counted on the STORED value: at the declared `scale` when one applies, else at the value's own decimal places (leading zeros never count) — so on a `currency` field, where `scale` is refused, an amount's written decimals count toward the total; a fraction-stored `percent` is counted two places further right (`scale + 2`, or 2 with no `scale`), which makes the count that of the percentage-point value as displayed. Not decimal places (that is `scale`; a currency's are its CLDR fraction digits) and not a column size: every numeric column keeps the platform's fixed exact decimal whatever this declares. Not read on any other field type. | | **scale** | `integer` | optional | Decimal places (integer 0-100). Applies to `number`, `percent`, `rating` and `slider` fields, where it is enforced on writes, and to a `formula` field, whose computed result is rounded to it — a `formula` whose `returnType` is `percent` counts the places of its displayed percentage, as a `percent` field does, so a fraction result is rounded to `scale + 2` places. REFUSED on a `currency` field — delete it there: a currency amount's decimal places are its currency's, so the currency's CLDR fraction digits decide how the amount displays, and a currency write's decimal places stay unconstrained. OMITTED on a `percent` field ⇒ 0 decimal places, so a stored 0.25 reads `25%` on every face; omitted on any OTHER numeric type declares NO fixed width — the value keeps its natural precision, and a DECLARED `scale: 0` (a year, a fiscal period, an ordinal) stays distinguishable from having declared nothing, so nothing is defaulted there. Consumers resolve the effective width by calling `resolveFieldScale` from `@objectstack/spec/data`, the single source for an absent `scale`: a renderer that spells its own fallback is a width no other face can see, and that is how one stored 0.25 came to read `25%` on the read-only cell and `25.00%` in the edit widget. On a `percent` field this is the number of decimal places of the PERCENTAGE-POINT value as displayed and entered — `scale: 2` means 12.34% — and the STORED precision derives from the field's storage scale rather than being declared again: a fraction-stored percent (no `max`, or a `max` at or below 1) stores 12.34% as 0.1234 and is allowed `scale + 2` decimal places at the write seam, while a whole-percent field (`max` above 1) stores the displayed number itself and is allowed exactly `scale`. `number`, `rating` and `slider` are allowed exactly `scale`. The upper bound is the platform's, not a policy: renderers turn `scale` into fraction digits through `toFixed` and `Intl.NumberFormat`'s `maximumFractionDigits`, both of which throw a RangeError above 100 — so a larger declaration is unrenderable by any conforming consumer. | | **min** | `number` | optional | Minimum value. Checked on the WRITTEN value only — the same transition-gate class as `requiredWhen`: an UPDATE validates just the fields the payload carries, so a stored value below a bound declared later is never re-read and survives unrelated edits; only a write that carries an out-of-bound value is refused, and a repairing write is accepted. For an invariant re-checked on every write, declare a `validations[]` `script` rule instead. | | **max** | `number` | optional | Maximum value. Checked on the WRITTEN value only — the same transition-gate class as `min`: a stored value above a bound declared later is never re-read and survives unrelated edits; only a write that carries an out-of-bound value is refused. For an invariant re-checked on every write, declare a `validations[]` `script` rule instead. | @@ -666,7 +666,7 @@ const result = ApiMethod.parse(data); | **minLength** | `integer` | optional | Min character length (positive integer; `minLength: 0` is refused — express "no minimum" by omitting the key). Only authorable on types that store a bounded string: text, textarea, email, url, phone, password, markdown, html, richtext, code, signature, qrcode. Checked on the WRITTEN value only (the `min`/`max` transition-gate class): a stored value shorter than a bound declared later is never re-read and survives unrelated edits — only a write carrying a too-short value is refused. | | **valueDomain** | `Enum<'iana_time_zone' \| 'iso_4217_currency' \| 'iso_3166_alpha2'>` | optional | Standard value domain the WRITTEN value must be a member of: `iana_time_zone` (an IANA/tzdb zone identifier such as `UTC`, `Asia/Kolkata`, `Europe/Kyiv` — membership is the `Intl.DateTimeFormat` probe, never the `Intl.supportedValuesOf` enumeration, which omits `UTC`), `iso_4217_currency` (an ISO 4217 alphabetic currency code, uppercase, e.g. `CHF`) or `iso_3166_alpha2` (an ISO 3166-1 alpha-2 country code, uppercase, e.g. `CH`). The same closed vocabulary and the same membership predicate as a settings specifier's `valueDomain`. Only authorable on `text` — the one type whose stored value is a single plain string naming the member. Checked on the WRITTEN value only (the `min`/`max`/`maxLength` transition-gate class): a stored value outside a domain declared later is never re-read and survives unrelated edits — only a write carrying a non-member is refused, with the field error code `value_domain`. Reach for it precisely where a pattern cannot help: `^[A-Z]{2}$` admits `ZZ`, and `Mars/Olympus` is a shape-valid zone that does not exist. | | **rows** | `integer` | optional | Height of the INLINE multiline editor, in text rows (positive integer — the HTML textarea `rows` attribute; fullscreen/dialog editor surfaces size themselves and ignore it). Only authorable on multiline editor types: textarea, markdown, html, richtext. Omit it for the widget default height. | -| **precision** | `integer` | optional | Total digits (non-negative integer) — the `p` of a DECIMAL(p, s): the digits of the value, integer and fraction together, counted at the field's decimal places, so `precision: 5, scale: 2` holds up to 999.99 and refuses 1234.5 (1234.50 is 6 digits). Enforced on writes of `number`, `currency`, `percent`, `rating` and `slider` fields: a value that needs more digits is refused with field code `max_precision`, never rounded. Counted on the STORED value: at the declared `scale` when one applies, else at the value's own decimal places (leading zeros never count) — so on a `currency` field, where `scale` is refused, an amount's written decimals count toward the total; a fraction-stored `percent` is counted two places further right (`scale + 2`, or 2 with no `scale`), which makes the count that of the percentage-point value as displayed. Not decimal places (that is `scale`; a currency's are its CLDR fraction digits) and not a column size: every numeric column keeps the platform's fixed exact decimal whatever this declares. Not read on any other field type. | +| **precision** | `integer` | optional | Total digits (positive integer; `precision: 0` is refused — it would admit only the value 0, and zero decimal places is `scale: 0`; omit the key for no digit limit) — the `p` of a DECIMAL(p, s): the digits of the value, integer and fraction together, counted at the field's decimal places, so `precision: 5, scale: 2` holds up to 999.99 and refuses 1234.5 (1234.50 is 6 digits). Enforced on writes of `number`, `currency`, `percent`, `rating` and `slider` fields: a value that needs more digits is refused with field code `max_precision`, never rounded. Counted on the STORED value: at the declared `scale` when one applies, else at the value's own decimal places (leading zeros never count) — so on a `currency` field, where `scale` is refused, an amount's written decimals count toward the total; a fraction-stored `percent` is counted two places further right (`scale + 2`, or 2 with no `scale`), which makes the count that of the percentage-point value as displayed. Not decimal places (that is `scale`; a currency's are its CLDR fraction digits) and not a column size: every numeric column keeps the platform's fixed exact decimal whatever this declares. Not read on any other field type. | | **scale** | `integer` | optional | Decimal places (integer 0-100). Applies to `number`, `percent`, `rating` and `slider` fields, where it is enforced on writes, and to a `formula` field, whose computed result is rounded to it — a `formula` whose `returnType` is `percent` counts the places of its displayed percentage, as a `percent` field does, so a fraction result is rounded to `scale + 2` places. REFUSED on a `currency` field — delete it there: a currency amount's decimal places are its currency's, so the currency's CLDR fraction digits decide how the amount displays, and a currency write's decimal places stay unconstrained. OMITTED on a `percent` field ⇒ 0 decimal places, so a stored 0.25 reads `25%` on every face; omitted on any OTHER numeric type declares NO fixed width — the value keeps its natural precision, and a DECLARED `scale: 0` (a year, a fiscal period, an ordinal) stays distinguishable from having declared nothing, so nothing is defaulted there. Consumers resolve the effective width by calling `resolveFieldScale` from `@objectstack/spec/data`, the single source for an absent `scale`: a renderer that spells its own fallback is a width no other face can see, and that is how one stored 0.25 came to read `25%` on the read-only cell and `25.00%` in the edit widget. On a `percent` field this is the number of decimal places of the PERCENTAGE-POINT value as displayed and entered — `scale: 2` means 12.34% — and the STORED precision derives from the field's storage scale rather than being declared again: a fraction-stored percent (no `max`, or a `max` at or below 1) stores 12.34% as 0.1234 and is allowed `scale + 2` decimal places at the write seam, while a whole-percent field (`max` above 1) stores the displayed number itself and is allowed exactly `scale`. `number`, `rating` and `slider` are allowed exactly `scale`. The upper bound is the platform's, not a policy: renderers turn `scale` into fraction digits through `toFixed` and `Intl.NumberFormat`'s `maximumFractionDigits`, both of which throw a RangeError above 100 — so a larger declaration is unrenderable by any conforming consumer. | | **min** | `number` | optional | Minimum value. Checked on the WRITTEN value only — the same transition-gate class as `requiredWhen`: an UPDATE validates just the fields the payload carries, so a stored value below a bound declared later is never re-read and survives unrelated edits; only a write that carries an out-of-bound value is refused, and a repairing write is accepted. For an invariant re-checked on every write, declare a `validations[]` `script` rule instead. | | **max** | `number` | optional | Maximum value. Checked on the WRITTEN value only — the same transition-gate class as `min`: a stored value above a bound declared later is never re-read and survives unrelated edits; only a write that carries an out-of-bound value is refused. For an invariant re-checked on every write, declare a `validations[]` `script` rule instead. | diff --git a/packages/metadata-protocol/src/protocol.publish-draft-schema-reparse.test.ts b/packages/metadata-protocol/src/protocol.publish-draft-schema-reparse.test.ts index 7533c96544f..9bee5f5ef8b 100644 --- a/packages/metadata-protocol/src/protocol.publish-draft-schema-reparse.test.ts +++ b/packages/metadata-protocol/src/protocol.publish-draft-schema-reparse.test.ts @@ -340,3 +340,63 @@ describe('[#35924] what the publish still promotes', () => { expect(rowsFor(rows, name)).toEqual([{ state: 'active', package_id: PKG }]); }); }); + +/** + * [#39173] A number field declaring `precision: 0` — a TOTAL digit count of + * 0, which admits only the value 0 — is refused at the save door and at the + * publish, with the spec's sentence that sends decimal places to `scale`. + * + * Measured once before the fix (an acceptance run of the in-app build agent): + * the draft save of `{ type: 'number', min: 0, precision: 0 }` answered + * publishable with no refusal, the publish answered published, and the seed + * load then refused 7 of 8 rows ("at most 0 digits in total"). These pins run + * the real save and publish paths over this file's stub engine; the refusal is + * `FieldSchema`'s, reached through the save door's parse and the #35924 + * re-parse of a stored draft. + */ +describe('[#39173] an object whose number field declares `precision: 0` is refused at save and at publish, naming `scale`', () => { + const withPrecision = (name: string, precision: number) => { + const body = sealedLedger(name); + return { ...body, fields: { ...body.fields, headcount: { type: 'number', label: 'Headcount', min: 0, precision } } }; + }; + const precisionIssues = (outcome: unknown) => + ((outcome as { issues?: Array<{ path: string; message: string }> }).issues ?? []).filter((i) => i.path === 'fields.headcount.precision'); + + it('the draft save refuses it: INVALID_METADATA / 422 at fields.headcount.precision, the sentence naming `scale: 0`; nothing stored', async () => { + const { engine, rows } = makeStubEngine(); + const protocol = new ObjectStackProtocolImplementation(engine); + const name = 'crm_ledger'; + const outcome = await protocol.saveMetaItem({ type: 'object', name, item: withPrecision(name, 0), packageId: PKG, mode: 'draft' }) + .then(() => 'saved', (e: unknown) => e); + + expect(outcome).toMatchObject({ code: 'INVALID_METADATA', status: 422 }); + const [issue] = precisionIssues(outcome); + expect(issue?.message).toContain('`scale: 0`'); + expect(rowsFor(rows, name)).toEqual([]); + }); + + it('a draft stored before the refusal landed is refused at the per-item publish; the draft stays a draft', async () => { + const { engine, rows } = makeStubEngine(); + const protocol = new ObjectStackProtocolImplementation(engine); + const name = 'crm_ledger'; + await stagePreRefinementDraft(protocol, rows, name, withPrecision(name, 0)); + + const outcome = await protocol.publishMetaItem({ type: 'object', name, packageId: PKG }) + .then(() => 'published', (e: unknown) => e); + + expect(outcome).toMatchObject({ code: 'INVALID_METADATA', status: 422 }); + const [issue] = precisionIssues(outcome); + expect(issue?.message).toContain('`scale: 0`'); + expect(rowsFor(rows, name)).toEqual([{ state: 'draft', package_id: PKG }]); + }); + + it('CONTROL — `precision: 1` saves and publishes as before', async () => { + const { engine, rows } = makeStubEngine(); + const protocol = new ObjectStackProtocolImplementation(engine); + const name = 'crm_ledger'; + await protocol.saveMetaItem({ type: 'object', name, item: withPrecision(name, 1), packageId: PKG, mode: 'draft' }); + + expect(await protocol.publishMetaItem({ type: 'object', name, packageId: PKG })).toMatchObject({ success: true }); + expect(rowsFor(rows, name)).toEqual([{ state: 'active', package_id: PKG }]); + }); +}); diff --git a/packages/spec/docs/references/system/migration.md b/packages/spec/docs/references/system/migration.md index 6b253c0cf78..5e4fc9433f4 100644 --- a/packages/spec/docs/references/system/migration.md +++ b/packages/spec/docs/references/system/migration.md @@ -67,7 +67,7 @@ Add a new field to an existing object | **minLength** | `integer` | optional | Min character length (positive integer; `minLength: 0` is refused — express "no minimum" by omitting the key). Only authorable on types that store a bounded string: text, textarea, email, url, phone, password, markdown, html, richtext, code, signature, qrcode. Checked on the WRITTEN value only (the `min`/`max` transition-gate class): a stored value shorter than a bound declared later is never re-read and survives unrelated edits — only a write carrying a too-short value is refused. | | **valueDomain** | `Enum<'iana_time_zone' \| 'iso_4217_currency' \| 'iso_3166_alpha2'>` | optional | Standard value domain the WRITTEN value must be a member of: `iana_time_zone` (an IANA/tzdb zone identifier such as `UTC`, `Asia/Kolkata`, `Europe/Kyiv` — membership is the `Intl.DateTimeFormat` probe, never the `Intl.supportedValuesOf` enumeration, which omits `UTC`), `iso_4217_currency` (an ISO 4217 alphabetic currency code, uppercase, e.g. `CHF`) or `iso_3166_alpha2` (an ISO 3166-1 alpha-2 country code, uppercase, e.g. `CH`). The same closed vocabulary and the same membership predicate as a settings specifier's `valueDomain`. Only authorable on `text` — the one type whose stored value is a single plain string naming the member. Checked on the WRITTEN value only (the `min`/`max`/`maxLength` transition-gate class): a stored value outside a domain declared later is never re-read and survives unrelated edits — only a write carrying a non-member is refused, with the field error code `value_domain`. Reach for it precisely where a pattern cannot help: `^[A-Z]{2}$` admits `ZZ`, and `Mars/Olympus` is a shape-valid zone that does not exist. | | **rows** | `integer` | optional | Height of the INLINE multiline editor, in text rows (positive integer — the HTML textarea `rows` attribute; fullscreen/dialog editor surfaces size themselves and ignore it). Only authorable on multiline editor types: textarea, markdown, html, richtext. Omit it for the widget default height. | -| **precision** | `integer` | optional | Total digits (non-negative integer) — the `p` of a DECIMAL(p, s): the digits of the value, integer and fraction together, counted at the field's decimal places, so `precision: 5, scale: 2` holds up to 999.99 and refuses 1234.5 (1234.50 is 6 digits). Enforced on writes of `number`, `currency`, `percent`, `rating` and `slider` fields: a value that needs more digits is refused with field code `max_precision`, never rounded. Counted on the STORED value: at the declared `scale` when one applies, else at the value's own decimal places (leading zeros never count) — so on a `currency` field, where `scale` is refused, an amount's written decimals count toward the total; a fraction-stored `percent` is counted two places further right (`scale + 2`, or 2 with no `scale`), which makes the count that of the percentage-point value as displayed. Not decimal places (that is `scale`; a currency's are its CLDR fraction digits) and not a column size: every numeric column keeps the platform's fixed exact decimal whatever this declares. Not read on any other field type. | +| **precision** | `integer` | optional | Total digits (positive integer; `precision: 0` is refused — it would admit only the value 0, and zero decimal places is `scale: 0`; omit the key for no digit limit) — the `p` of a DECIMAL(p, s): the digits of the value, integer and fraction together, counted at the field's decimal places, so `precision: 5, scale: 2` holds up to 999.99 and refuses 1234.5 (1234.50 is 6 digits). Enforced on writes of `number`, `currency`, `percent`, `rating` and `slider` fields: a value that needs more digits is refused with field code `max_precision`, never rounded. Counted on the STORED value: at the declared `scale` when one applies, else at the value's own decimal places (leading zeros never count) — so on a `currency` field, where `scale` is refused, an amount's written decimals count toward the total; a fraction-stored `percent` is counted two places further right (`scale + 2`, or 2 with no `scale`), which makes the count that of the percentage-point value as displayed. Not decimal places (that is `scale`; a currency's are its CLDR fraction digits) and not a column size: every numeric column keeps the platform's fixed exact decimal whatever this declares. Not read on any other field type. | | **scale** | `integer` | optional | Decimal places (integer 0-100). Applies to `number`, `percent`, `rating` and `slider` fields, where it is enforced on writes, and to a `formula` field, whose computed result is rounded to it — a `formula` whose `returnType` is `percent` counts the places of its displayed percentage, as a `percent` field does, so a fraction result is rounded to `scale + 2` places. REFUSED on a `currency` field — delete it there: a currency amount's decimal places are its currency's, so the currency's CLDR fraction digits decide how the amount displays, and a currency write's decimal places stay unconstrained. OMITTED on a `percent` field ⇒ 0 decimal places, so a stored 0.25 reads `25%` on every face; omitted on any OTHER numeric type declares NO fixed width — the value keeps its natural precision, and a DECLARED `scale: 0` (a year, a fiscal period, an ordinal) stays distinguishable from having declared nothing, so nothing is defaulted there. Consumers resolve the effective width by calling `resolveFieldScale` from `@objectstack/spec/data`, the single source for an absent `scale`: a renderer that spells its own fallback is a width no other face can see, and that is how one stored 0.25 came to read `25%` on the read-only cell and `25.00%` in the edit widget. On a `percent` field this is the number of decimal places of the PERCENTAGE-POINT value as displayed and entered — `scale: 2` means 12.34% — and the STORED precision derives from the field's storage scale rather than being declared again: a fraction-stored percent (no `max`, or a `max` at or below 1) stores 12.34% as 0.1234 and is allowed `scale + 2` decimal places at the write seam, while a whole-percent field (`max` above 1) stores the displayed number itself and is allowed exactly `scale`. `number`, `rating` and `slider` are allowed exactly `scale`. The upper bound is the platform's, not a policy: renderers turn `scale` into fraction digits through `toFixed` and `Intl.NumberFormat`'s `maximumFractionDigits`, both of which throw a RangeError above 100 — so a larger declaration is unrenderable by any conforming consumer. | | **min** | `number` | optional | Minimum value. Checked on the WRITTEN value only — the same transition-gate class as `requiredWhen`: an UPDATE validates just the fields the payload carries, so a stored value below a bound declared later is never re-read and survives unrelated edits; only a write that carries an out-of-bound value is refused, and a repairing write is accepted. For an invariant re-checked on every write, declare a `validations[]` `script` rule instead. | | **max** | `number` | optional | Maximum value. Checked on the WRITTEN value only — the same transition-gate class as `min`: a stored value above a bound declared later is never re-read and survives unrelated edits; only a write that carries an out-of-bound value is refused. For an invariant re-checked on every write, declare a `validations[]` `script` rule instead. | diff --git a/packages/spec/docs/references/system/migration.part-2.md b/packages/spec/docs/references/system/migration.part-2.md index 1cb7541d1d3..7685d99bb1d 100644 --- a/packages/spec/docs/references/system/migration.part-2.md +++ b/packages/spec/docs/references/system/migration.part-2.md @@ -46,7 +46,7 @@ Add a new field to an existing object | **minLength** | `integer` | optional | Min character length (positive integer; `minLength: 0` is refused — express "no minimum" by omitting the key). Only authorable on types that store a bounded string: text, textarea, email, url, phone, password, markdown, html, richtext, code, signature, qrcode. Checked on the WRITTEN value only (the `min`/`max` transition-gate class): a stored value shorter than a bound declared later is never re-read and survives unrelated edits — only a write carrying a too-short value is refused. | | **valueDomain** | `Enum<'iana_time_zone' \| 'iso_4217_currency' \| 'iso_3166_alpha2'>` | optional | Standard value domain the WRITTEN value must be a member of: `iana_time_zone` (an IANA/tzdb zone identifier such as `UTC`, `Asia/Kolkata`, `Europe/Kyiv` — membership is the `Intl.DateTimeFormat` probe, never the `Intl.supportedValuesOf` enumeration, which omits `UTC`), `iso_4217_currency` (an ISO 4217 alphabetic currency code, uppercase, e.g. `CHF`) or `iso_3166_alpha2` (an ISO 3166-1 alpha-2 country code, uppercase, e.g. `CH`). The same closed vocabulary and the same membership predicate as a settings specifier's `valueDomain`. Only authorable on `text` — the one type whose stored value is a single plain string naming the member. Checked on the WRITTEN value only (the `min`/`max`/`maxLength` transition-gate class): a stored value outside a domain declared later is never re-read and survives unrelated edits — only a write carrying a non-member is refused, with the field error code `value_domain`. Reach for it precisely where a pattern cannot help: `^[A-Z]{2}$` admits `ZZ`, and `Mars/Olympus` is a shape-valid zone that does not exist. | | **rows** | `integer` | optional | Height of the INLINE multiline editor, in text rows (positive integer — the HTML textarea `rows` attribute; fullscreen/dialog editor surfaces size themselves and ignore it). Only authorable on multiline editor types: textarea, markdown, html, richtext. Omit it for the widget default height. | -| **precision** | `integer` | optional | Total digits (non-negative integer) — the `p` of a DECIMAL(p, s): the digits of the value, integer and fraction together, counted at the field's decimal places, so `precision: 5, scale: 2` holds up to 999.99 and refuses 1234.5 (1234.50 is 6 digits). Enforced on writes of `number`, `currency`, `percent`, `rating` and `slider` fields: a value that needs more digits is refused with field code `max_precision`, never rounded. Counted on the STORED value: at the declared `scale` when one applies, else at the value's own decimal places (leading zeros never count) — so on a `currency` field, where `scale` is refused, an amount's written decimals count toward the total; a fraction-stored `percent` is counted two places further right (`scale + 2`, or 2 with no `scale`), which makes the count that of the percentage-point value as displayed. Not decimal places (that is `scale`; a currency's are its CLDR fraction digits) and not a column size: every numeric column keeps the platform's fixed exact decimal whatever this declares. Not read on any other field type. | +| **precision** | `integer` | optional | Total digits (positive integer; `precision: 0` is refused — it would admit only the value 0, and zero decimal places is `scale: 0`; omit the key for no digit limit) — the `p` of a DECIMAL(p, s): the digits of the value, integer and fraction together, counted at the field's decimal places, so `precision: 5, scale: 2` holds up to 999.99 and refuses 1234.5 (1234.50 is 6 digits). Enforced on writes of `number`, `currency`, `percent`, `rating` and `slider` fields: a value that needs more digits is refused with field code `max_precision`, never rounded. Counted on the STORED value: at the declared `scale` when one applies, else at the value's own decimal places (leading zeros never count) — so on a `currency` field, where `scale` is refused, an amount's written decimals count toward the total; a fraction-stored `percent` is counted two places further right (`scale + 2`, or 2 with no `scale`), which makes the count that of the percentage-point value as displayed. Not decimal places (that is `scale`; a currency's are its CLDR fraction digits) and not a column size: every numeric column keeps the platform's fixed exact decimal whatever this declares. Not read on any other field type. | | **scale** | `integer` | optional | Decimal places (integer 0-100). Applies to `number`, `percent`, `rating` and `slider` fields, where it is enforced on writes, and to a `formula` field, whose computed result is rounded to it — a `formula` whose `returnType` is `percent` counts the places of its displayed percentage, as a `percent` field does, so a fraction result is rounded to `scale + 2` places. REFUSED on a `currency` field — delete it there: a currency amount's decimal places are its currency's, so the currency's CLDR fraction digits decide how the amount displays, and a currency write's decimal places stay unconstrained. OMITTED on a `percent` field ⇒ 0 decimal places, so a stored 0.25 reads `25%` on every face; omitted on any OTHER numeric type declares NO fixed width — the value keeps its natural precision, and a DECLARED `scale: 0` (a year, a fiscal period, an ordinal) stays distinguishable from having declared nothing, so nothing is defaulted there. Consumers resolve the effective width by calling `resolveFieldScale` from `@objectstack/spec/data`, the single source for an absent `scale`: a renderer that spells its own fallback is a width no other face can see, and that is how one stored 0.25 came to read `25%` on the read-only cell and `25.00%` in the edit widget. On a `percent` field this is the number of decimal places of the PERCENTAGE-POINT value as displayed and entered — `scale: 2` means 12.34% — and the STORED precision derives from the field's storage scale rather than being declared again: a fraction-stored percent (no `max`, or a `max` at or below 1) stores 12.34% as 0.1234 and is allowed `scale + 2` decimal places at the write seam, while a whole-percent field (`max` above 1) stores the displayed number itself and is allowed exactly `scale`. `number`, `rating` and `slider` are allowed exactly `scale`. The upper bound is the platform's, not a policy: renderers turn `scale` into fraction digits through `toFixed` and `Intl.NumberFormat`'s `maximumFractionDigits`, both of which throw a RangeError above 100 — so a larger declaration is unrenderable by any conforming consumer. | | **min** | `number` | optional | Minimum value. Checked on the WRITTEN value only — the same transition-gate class as `requiredWhen`: an UPDATE validates just the fields the payload carries, so a stored value below a bound declared later is never re-read and survives unrelated edits; only a write that carries an out-of-bound value is refused, and a repairing write is accepted. For an invariant re-checked on every write, declare a `validations[]` `script` rule instead. | | **max** | `number` | optional | Maximum value. Checked on the WRITTEN value only — the same transition-gate class as `min`: a stored value above a bound declared later is never re-read and survives unrelated edits; only a write that carries an out-of-bound value is refused. For an invariant re-checked on every write, declare a `validations[]` `script` rule instead. | diff --git a/packages/spec/src/data/currency-precision-iso4217.test.ts b/packages/spec/src/data/currency-precision-iso4217.test.ts index f2a8b19041d..38e0d524449 100644 --- a/packages/spec/src/data/currency-precision-iso4217.test.ts +++ b/packages/spec/src/data/currency-precision-iso4217.test.ts @@ -231,8 +231,10 @@ describe('field-level `precision` is total digits, never judged against the curr }); it('accepts a total-digit count on every fraction-digit class (0: JPY, 2: USD, 3: KWD)', () => { + // The smallest legal count is 1: `precision: 0` admits only the value 0 + // and is refused at parse (#39173), on a currency field as on any other. const cases: Array<[string, number]> = [ - ['JPY', 0], ['JPY', 12], ['USD', 2], ['USD', 18], ['KWD', 3], ['KWD', 2], ['KWD', 15], + ['JPY', 1], ['JPY', 12], ['USD', 2], ['USD', 18], ['KWD', 3], ['KWD', 2], ['KWD', 15], ]; for (const [code, precision] of cases) { const result = FieldSchema.safeParse({ ...base, precision, currencyConfig: fixed(code) }); diff --git a/packages/spec/src/data/field.test.ts b/packages/spec/src/data/field.test.ts index 5f6521daebf..fbfec3f181b 100644 --- a/packages/spec/src/data/field.test.ts +++ b/packages/spec/src/data/field.test.ts @@ -326,18 +326,24 @@ describe('FieldSchema', () => { const issue = result.error.issues.find((i) => i.path[0] === key); expect(issue?.code).toBe(code); // Message substance, not just a throw: the refusal names what a - // legal value looks like (int / >=0), so an AI author can fix it. - expect(issue?.message).toMatch(code === 'invalid_type' ? /expected int/ : />=0/); + // legal value looks like (int / >=0 for `scale`; a positive + // integer for `precision`, whose lower bound is 1 since #39173), + // so an AI author can fix it. + const legalShape = code === 'invalid_type' ? /expected int/ : key === 'precision' ? /positive integer/ : />=0/; + expect(issue?.message).toMatch(legalShape); } }); } - it('every legal declaration still parses byte-identically (0, positive integers, absent)', () => { + it('every legal declaration still parses byte-identically (`scale: 0`, positive integers, absent)', () => { + // `precision: 0` left this list with #39173: a total digit count of 0 + // admits only the value 0, so it is refused (pinned in the #39173 + // describe at the end of this file). for (const legal of [ { scale: 0 }, { scale: 2 }, { precision: 18, scale: 2 }, - { precision: 0 }, + { precision: 1 }, {}, // neither declared — no new default appears ]) { const input = { name: 'amount', label: 'Amount', type: 'number' as const, ...legal }; @@ -3140,3 +3146,77 @@ describe('select option value: one character is enough', () => { expect(issueAt(SnakeCaseIdentifierSchema.safeParse('a'), '')).toBe('too_small'); }); }); + +/** + * #39173 — `precision` is the TOTAL digit count (the `p` of a DECIMAL(p, s)), + * enforced on writes as `max_precision`. `precision: 0` therefore admits only + * the value 0: measured once in an acceptance run, an in-app build agent saved + * `{ type: 'number', min: 0, precision: 0 }`, the draft save and the publish + * both answered clean, and 7 of 8 seed rows were then refused ("at most 0 + * digits in total"). The author meant zero DECIMAL places, which is `scale`. + * The producer now refuses the one value that is never usable, with a sentence + * that names `scale`, so the save door and the publish (which both run this + * parse) refuse it loudly instead of every later write. + */ +describe('FieldSchema — `precision: 0` is refused at authoring, naming `scale` (#39173)', () => { + const PRECISION_TYPES = ['number', 'currency', 'percent', 'rating', 'slider'] as const; + + const precisionIssue = (result: ReturnType) => + result.success ? undefined : result.error.issues.find((i) => i.path.join('.') === 'precision'); + + it('refuses the measured body `{ type: number, min: 0, precision: 0 }` with a too_small issue at [precision] whose message names `scale`', () => { + const issue = precisionIssue(FieldSchema.safeParse({ name: 'employees', label: 'Employees', type: 'number', min: 0, precision: 0 })); + expect(issue?.code).toBe('too_small'); + // The sentence is the author's prescription: it says what `precision` + // counts and sends decimal places to `scale`. + expect(issue?.message).toMatch(/total digit count/); + expect(issue?.message).toContain('`scale: 0`'); + }); + + for (const type of PRECISION_TYPES) { + it(`refuses \`precision: 0\` on a \`${type}\` field`, () => { + expect(precisionIssue(FieldSchema.safeParse({ name: 'amount', label: 'Amount', type, precision: 0 }))?.code).toBe('too_small'); + }); + } + + it('on a `currency` field the sentence sends the author to delete `precision`, since `scale` is refused there too', () => { + const issue = precisionIssue(FieldSchema.safeParse({ name: 'amount', label: 'Amount', type: 'currency', precision: 0 })); + expect(issue?.code).toBe('too_small'); + expect(issue?.message).toContain('on a `currency` field they are the currency\'s own and `scale` is refused there, so delete `precision`'); + // The prescription holds on that type: without `precision` the field parses. + expect(FieldSchema.safeParse({ name: 'amount', label: 'Amount', type: 'currency' }).success).toBe(true); + expect(FieldSchema.safeParse({ name: 'amount', label: 'Amount', type: 'currency', precision: 12 }).success).toBe(true); + }); + + it('the object parse (what the save door and the publish run) refuses it at fields.FIELD.precision', () => { + const result = ObjectSchema.safeParse({ + name: 'hr_department', + label: 'Department', + fields: { employees: { type: 'number', label: 'Employees', min: 0, precision: 0 } }, + }); + expect(result.success).toBe(false); + if (!result.success) { + const issue = result.error.issues.find((i) => i.path.join('.') === 'fields.employees.precision'); + expect(issue?.code).toBe('too_small'); + expect(issue?.message).toContain('`scale: 0`'); + } + }); + + it('an authored `precision >= 1` parses byte-identically to before; `scale: 0` alone stays the whole-number spelling', () => { + for (const legal of [ + { precision: 1 }, + { precision: 1, scale: 2 }, + { precision: 18, scale: 2 }, + { precision: 10, scale: 0 }, + { scale: 0 }, + ]) { + const input = { name: 'amount', label: 'Amount', type: 'number' as const, min: 0, ...legal }; + const result = FieldSchema.safeParse(input); + expect(result.success).toBe(true); + if (result.success) { + expect(result.data.precision).toBe((legal as { precision?: number }).precision); + expect(result.data.scale).toBe((legal as { scale?: number }).scale); + } + } + }); +}); diff --git a/packages/spec/src/data/field.zod.ts b/packages/spec/src/data/field.zod.ts index 61d258bd3fd..f69db3a51a0 100644 --- a/packages/spec/src/data/field.zod.ts +++ b/packages/spec/src/data/field.zod.ts @@ -1319,6 +1319,27 @@ const SCALE_UPPER_BOUND_MESSAGE = + 'declaration is unrenderable rather than merely large. Declare at most 100 (an IEEE-754 ' + 'double carries ~17 significant digits, so a meaningful display precision is far below it).'; +/** + * The refusal text for a `precision` below 1. `precision` is the TOTAL digit + * count, so `0` admits only the value 0 and every other write is refused with + * `max_precision`; the measured author of `precision: 0` meant zero DECIMAL + * places. Where decimal places live depends on the type, and the sentence says + * both: `scale` on `number` / `percent` / `rating` / `slider`, the currency's + * own on a `currency` field, where `scale` is refused (see the currency `scale` + * refusal in the superRefine below) — so the author (an AI build agent + * included) can fix the declaration from the refusal alone on every type the + * key applies to. A negative value takes the same sentence: it is no more a + * digit count than 0 is. + */ +const PRECISION_LOWER_BOUND_MESSAGE = + '`precision` must be a positive integer: it is the total digit count of the value (the `p` of a ' + + 'DECIMAL(p, s), integer and decimal digits together), so `precision: 0` would admit only the value 0 ' + + 'and refuse every other write. Decimal places are not declared with `precision`: on a `number`, ' + + '`percent`, `rating` or `slider` field they are `scale` (a whole number declares `scale: 0`, not ' + + '`precision: 0`); on a `currency` field they are the currency\'s own and `scale` is refused there, so ' + + 'delete `precision`. Declare the total digits (`precision: 10, scale: 2` holds up to 99999999.99; on a ' + + 'currency, `precision: 12`), or omit `precision` for no digit limit.'; + /** * What `z.array(z.any())` cost on the two explicit column lists below (#9227): * every column object validated — right keys, wrong keys, misspelled keys, @@ -1961,7 +1982,14 @@ export const FieldSchema = lazySchema(() => { // stays the fixed exact decimal of NUMERIC_COLUMN_REPRESENTATION. The // describe states the counting rule because the field reference page is // generated from it, and that page is what an author reads. - precision: z.number().int().min(0).optional().describe('Total digits (non-negative integer) — the `p` of a DECIMAL(p, s): the digits of the value, integer and fraction together, counted at the field\'s decimal places, so `precision: 5, scale: 2` holds up to 999.99 and refuses 1234.5 (1234.50 is 6 digits). Enforced on writes of `number`, `currency`, `percent`, `rating` and `slider` fields: a value that needs more digits is refused with field code `max_precision`, never rounded. Counted on the STORED value: at the declared `scale` when one applies, else at the value\'s own decimal places (leading zeros never count) — so on a `currency` field, where `scale` is refused, an amount\'s written decimals count toward the total; a fraction-stored `percent` is counted two places further right (`scale + 2`, or 2 with no `scale`), which makes the count that of the percentage-point value as displayed. Not decimal places (that is `scale`; a currency\'s are its CLDR fraction digits) and not a column size: every numeric column keeps the platform\'s fixed exact decimal whatever this declares. Not read on any other field type.'), + // #39173 — the lower bound is 1, not 0: a TOTAL digit count of 0 admits + // only the value 0, so `precision: 0` published clean and then refused every + // non-zero write (`max_precision`). Measured: an in-app build agent wrote it + // meaning zero decimal places, which is `scale: 0`. "No digit limit" is + // expressed by omitting the key (the `minLength` lower-bound argument above), + // so the never-usable value is refused at authoring with the prescription + // ({@link PRECISION_LOWER_BOUND_MESSAGE}). + precision: z.number().int().min(1, { message: PRECISION_LOWER_BOUND_MESSAGE }).optional().describe('Total digits (positive integer; `precision: 0` is refused — it would admit only the value 0, and zero decimal places is `scale: 0`; omit the key for no digit limit) — the `p` of a DECIMAL(p, s): the digits of the value, integer and fraction together, counted at the field\'s decimal places, so `precision: 5, scale: 2` holds up to 999.99 and refuses 1234.5 (1234.50 is 6 digits). Enforced on writes of `number`, `currency`, `percent`, `rating` and `slider` fields: a value that needs more digits is refused with field code `max_precision`, never rounded. Counted on the STORED value: at the declared `scale` when one applies, else at the value\'s own decimal places (leading zeros never count) — so on a `currency` field, where `scale` is refused, an amount\'s written decimals count toward the total; a fraction-stored `percent` is counted two places further right (`scale + 2`, or 2 with no `scale`), which makes the count that of the percentage-point value as displayed. Not decimal places (that is `scale`; a currency\'s are its CLDR fraction digits) and not a column size: every numeric column keeps the platform\'s fixed exact decimal whatever this declares. Not read on any other field type.'), // #18972 — and an UPPER bound, for the same declared=enforced reason one // axis over: `scale` is unrenderable above 100 at every consumer, so a // larger declaration could only ever crash a reader. See diff --git a/packages/spec/src/migrations/entries/semantic/19.field-precision-zero-refused.ts b/packages/spec/src/migrations/entries/semantic/19.field-precision-zero-refused.ts new file mode 100644 index 00000000000..a27799a0c31 --- /dev/null +++ b/packages/spec/src/migrations/entries/semantic/19.field-precision-zero-refused.ts @@ -0,0 +1,37 @@ +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. + +import type { SemanticMigration } from '../../types.js'; + +// The upgrade-guide entry of a field declaring `precision: 0`, refused at +// authoring (accept-set narrowing; `FieldSchema.precision` is now +// `z.number().int().min(1)`). No conversion: deleting the key would make every +// value writable, changing what the field accepts, and whether the author meant +// "zero decimal places" (`scale: 0`) or a real digit bound is theirs to say. +export const entry: SemanticMigration = { + id: 'field-precision-zero-refused', + surface: + 'object field `precision: 0` — on `number`, `currency`, `percent`, `rating` or `slider` (or any type) in ' + + '`defineStack({ objects })`, an object extension\'s fields, an `object` metadata item or a standalone `field` ' + + 'metadata item saved through the metadata save door', + replacement: + 'On a `number`, `percent`, `rating` or `slider` field meant as a whole number, `scale: 0` and no `precision` (or ' + + 'a positive `precision` such as `precision: 10, scale: 0`); on a `currency` field, delete `precision`: its ' + + 'decimal places are the currency\'s own and `scale` is refused there; ' + + 'for a digit bound, the total digit count as a positive integer (`precision: 10, scale: 2` holds up to ' + + '99999999.99); for no digit limit, no `precision` at all', + reason: + '`precision` is the TOTAL digit count of the value (the `p` of a DECIMAL(p, s)), enforced on writes as ' + + '`max_precision`, so a field declaring `precision: 0` accepted only the value 0 and refused every other write ' + + '("at most 0 digits in total"), after a save and a publish that both answered clean. The declaration is ' + + 'measurably written by authors who meant zero decimal places, which is `scale: 0`. The schema now refuses it ' + + 'at `os validate`, `defineStack`, the metadata save door and the publish, with a sentence that names `scale`. ' + + 'A stored object row is not rewritten: an active row keeps loading and keeps its old write behaviour until the ' + + 'object is next saved, and that save, like the publish of a draft stored before this release, is refused until ' + + 'the declaration is fixed. There is no mechanical rewrite: dropping the key would silently make every value ' + + 'writable, and only the author knows whether they meant `scale: 0` or a real digit bound', + acceptanceCriteria: + 'No field declares `precision: 0`; `os validate` passes; each former `precision: 0` field accepts the values the ' + + 'author intends (a whole-number field refuses a fractional write with `max_scale` under `scale: 0`), and a ' + + 'field declaring a positive `precision` behaves exactly as before.', + relevantWhen: { kind: 'stack-declares', keys: ['objects', 'objectExtensions'] }, +}; diff --git a/packages/spec/src/migrations/semantic-relevance-coverage-38929.test.ts b/packages/spec/src/migrations/semantic-relevance-coverage-38929.test.ts index fe42364540f..95cc269f8a1 100644 --- a/packages/spec/src/migrations/semantic-relevance-coverage-38929.test.ts +++ b/packages/spec/src/migrations/semantic-relevance-coverage-38929.test.ts @@ -293,6 +293,7 @@ const LIST_VIEW = [ const FORM_VIEW = ['internal-record-forms-not-loaded', 'ui-form-view-modal-size-retired', 'ui-form-view-subforms-retired']; const FIELD = [ 'field-inline-edit-boolean-retired', + 'field-precision-zero-refused', 'lookup-depends-on-param-computed-field-refused', 'lookup-depends-on-param-computed-field-refused-on-tree', 'lookup-filter-record-path-structured-refused', diff --git a/packages/spec/src/migrations/semantic-relevance.test.ts b/packages/spec/src/migrations/semantic-relevance.test.ts index a359eb52cee..d98f7c09cd2 100644 --- a/packages/spec/src/migrations/semantic-relevance.test.ts +++ b/packages/spec/src/migrations/semantic-relevance.test.ts @@ -82,6 +82,7 @@ const EXPECTED_RELEVANCE: Readonly> = { '19:compose-stacks-job-name-across-packages-refused': ['jobs'], '19:field-group-shadowed-six-highlights': ['objects', 'objectExtensions'], '19:field-inline-edit-boolean-retired': ['objects', 'objectExtensions'], + '19:field-precision-zero-refused': ['objects', 'objectExtensions'], '19:flow-concurrent-branch-durable-pause-refused': ['flows'], '19:flow-condition-bare-ordering-before-record-date-arithmetic-refused': ['flows'], '19:flow-condition-mixed-or-index-spelling-date-text-refused': ['flows'],