Repository navigation
Description
Package
@storyblok/schema, storyblok (CLI)
Bug Description
There are two related problems with datasource dimensions in the schema workflow:
1. defineDatasource() typing doesn't match the docs.
The defineDatasource() docs show dimensions declared with only name and entry_value. The types, however, require id on every dimension, so the documented example doesn't compile.
Datasource reuses the generated CAPI response type, where Dimension.id: number is required. DATASOURCE_DEFAULTS only makes the top-level id, created_at, updated_at and dimensions optional, not the fields of each dimension.
The CLI already strips dimension ids before diffing (DATASOURCE_DIMENSION_STRIP_KEYS) and pushing (toDatasourceCreate only sends name and entry_value). Because of that, users have to enter a placeholder such as id: 0 just to satisfy TypeScript, and the CLI ignores it.
2. Updating a datasource that already has dimensions fails with dimensions.entry_value: has already been taken.
toDatasourceUpdate sends all local dimensions as dimensions_attributes entries with only { name, entry_value }. It doesn't attach the id of the matching remote dimension. With nested attributes, an entry without an id is treated as a new record, so dimensions that already exist get created again and the uniqueness check on entry_value fails.
Setting id in the schema doesn't help, because the CLI strips it (see above). Workarounds such as id: 0 therefore only fix the type error and don't affect the push.
This happens for any update to a datasource that already has dimensions. Examples are adding a new dimension, or renaming a dimension, which also ends up as destroy + create with the same entry_value because local and remote dimensions are matched on name::entry_value.
Steps to Reproduce
Typing
import { defineDatasource } from '@storyblok/schema';
// Example copied from the docs
export const colorsDatasource = defineDatasource({
name: 'Colors',
slug: 'colors',
dimensions: [
{ name: 'Light Mode', entry_value: 'light' },
{ name: 'Dark Mode', entry_value: 'dark' },
],
});tsc --noEmit --strict reports:
error TS2741: Property 'id' is missing in type '{ name: string; entry_value: string; }' but required in type 'Dimension'.
Push
- Define a datasource with dimensions (for example the one above, with
id: 0added to satisfy the types) and runstoryblok schema push. The datasource is created. - Add another dimension, e.g.
{ name: 'High Contrast', entry_value: 'contrast' }. - Run
storyblok schema pushagain. The datasource update fails.
Expected Behavior
- The documented
defineDatasource()example type-checks: dimensionid(anddatasource_id,created_at,updated_at) are optional in the input type, because they are assigned by the API and ignored by the CLI. storyblok schema pushupdates a datasource that already has dimensions. Existing dimensions are matched to their remote counterparts (e.g. byentry_value) and sent with their remoteid; only new dimensions are sent without anid.
Actual Behavior
- The documented example fails to type-check unless a dummy
idis added to each dimension. - Pushing an update to a datasource that already has dimensions fails with
dimensions.entry_value: has already been taken.
Environment
@storyblok/schema: 0.7.0
storyblok (CLI): 4.26.1The code referenced above is identical on main (89b9875).
Error Logs
Output of storyblok schema push (datasource name replaced; each error is repeated once per affected dimension):
▲ error Failed to update datasource <datasource>
▲ error dimensions.entry_value: has already been taken
▲ error dimensions.name: has already been taken
▲ error API Error: The request was well-formed but was unable to be followed due to semantic errors {
name: 'API Error',
message: 'Failed to update datasource <datasource>',
httpCode: 422,
cause: 'The request was well-formed but was unable to be followed due to semantic errors',
errorId: 'unprocessable_entity',
responseData: {
'dimensions.entry_value': [ 'has already been taken', ... ],
'dimensions.name': [ 'has already been taken', ... ]
}
}The error counts match the payload built by toDatasourceUpdate exactly:
| Local dimension vs. remote | Errors per dimension |
|---|---|
Unchanged (same name and entry_value), sent without id |
entry_value + name taken |
Renamed (same entry_value, new name), sent without id |
entry_value taken |
New entry_value |
none |
The renamed dimensions fail even though the old remote dimensions are sent with _destroy: true in the same request. The uniqueness validation still sees the existing rows, so destroy + re-create can't be used to rename a dimension.
Additional Context
Possible fixes:
- Schema: override
dimensionsin the input type with a dedicated input shape, e.g.Array<Pick<Dimension, 'name' | 'entry_value'> & Partial<Dimension>>, so the docs example compiles. - CLI: in
toDatasourceUpdate, resolve each local dimension againstremote.dimensions(byentry_value) and include the remoteid. That way existing dimensions are updated, including renames, instead of being created again. The same function is used byschema rollback, so rollback is likely affected too.
Activity
fix(schema,cli): preview and guard datasource dimension updates
fix(schema,cli): refuse dimensions a push cannot apply cleanly
fix(schema,cli): update datasources that already have dimensions
fix(schema,cli): preview and guard datasource dimension updates
fix(schema,cli): refuse dimensions a push cannot apply cleanly