Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Theme Checks] Added in a check for theme block settings validation #620

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/chilled-bugs-juggle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@shopify/theme-check-common': minor
'theme-check-vscode': minor
---

Add the `ValidPresetAndDefaultSettings` check. This check will allow us to validate that the settings defined in sections and blocks are valid. They are valid if they exist in either the block settings or the section settings.
2 changes: 2 additions & 0 deletions packages/theme-check-common/src/checks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import { ValidSchemaName } from './valid-schema-name';
import { ValidStaticBlockType } from './valid-static-block-type';
import { VariableName } from './variable-name';
import { MissingSchema } from './missing-schema';
import { ValidPresetAndDefaultSettings } from './valid-preset-and-default-settings';

export const allChecks: (LiquidCheckDefinition | JSONCheckDefinition)[] = [
AppBlockValidTags,
Expand Down Expand Up @@ -92,6 +93,7 @@ export const allChecks: (LiquidCheckDefinition | JSONCheckDefinition)[] = [
ValidStaticBlockType,
VariableName,
ValidSchemaName,
ValidPresetAndDefaultSettings,
];

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,303 @@
import { describe, expect, it } from 'vitest';
import { check } from '../../test/test-helper';
import { MockTheme } from '../../test/MockTheme';
import {
horizonNestedBlockSchema,
createHorizonBlockSchema,
createDawnSectionSchema,
validDawnDefaultSchema,
validDawnPresetSchema,
validHorizonPresetSchema,
validHorizonDefaultSchema,
} from './valid-schema-utils';
import { ValidPresetAndDefaultSettings } from '.';

const invalidBlockPresetSchema = {
name: 'Tabbed content',
settings: {
invalid_setting: 'this is an invalid setting as this key does not exist',
},
blocks: [
{
type: '_tab',
settings: {
title: 'Tab 1',
},
blocks: [
{
type: 'text',
},
],
},
{
type: '_tab',
settings: {
title: 'Tab 2',
invalid_nested_setting: 'this is an invalid setting as this key does not exist',
},
blocks: [
{
type: 'text',
settings: {
invalid_nested_setting_1: 'this is an invalid setting as this key does not exist',
},
},
],
},
{
type: '_tab',
settings: {
title: 'Tab 3',
},
blocks: [
{
type: 'text',
blocks: [
{
type: 'text',
settings: {
invalid_nested_setting_2: 'this is an invalid setting as this key does not exist',
},
},
],
},
],
},
],
};

const invalidBlockDefaultSchema = {
settings: {
invalid_setting: 'this is an invalid setting as this key does not exist',
},
blocks: [
{ type: '_tab', settings: { title: 'Tab 1' }, blocks: [{ type: 'text' }] },
{ type: '_tab', settings: { title: 'Tab 2' }, blocks: [{ type: 'text' }] },
{
type: '_tab',
settings: {
title: 'Tab 3',
invalid_nested_setting_1: 'this is an invalid setting as this key does not exist',
},
blocks: [
{
type: 'text',
settings: {
invalid_nested_setting_2: 'this is an invalid setting as this key does not exist',
},
},
],
},
],
};

const invalidSectionPresetSchema = {
name: 't:sections.announcement-bar.presets.name',
settings: {
invalid_setting: 'this is an invalid setting as this key does not exist',
},
blocks: [
{
type: 'announcement',
settings: {
invalid_nested_setting_1: 'this is an invalid setting as this key does not exist',
},
blocks: [
{
type: 'text',
settings: {
invalid_nested_setting_3: 'this is an invalid setting as this key does not exist',
},
},
],
},
{
type: '_tab',
settings: {
title: 'Tab 1',
invalid_nested_setting_2: 'this is an invalid setting as this key does not exist',
},
blocks: [
{
type: 'text',
},
],
},
],
};

const invalidSectionDefaultSchema = {
settings: {
invalid_setting: 'this is an invalid setting as this key does not exist',
},
blocks: [
{
type: 'announcement',
settings: {
title: 'Tab 1',
invalid_nested_setting_1: 'this is an invalid setting as this key does not exist',
},
blocks: [
{
type: 'text',
settings: {
invalid_nested_setting_2: 'this is an invalid setting as this key does not exist',
},
},
{
type: 'text',
settings: {
invalid_nested_setting_3: 'this is an invalid setting as this key does not exist',
},
blocks: [
{
type: 'text',
settings: {
invalid_nested_setting_4: 'this is an invalid setting as this key does not exist',
},
},
],
},
],
},
],
};

describe('ValidPresetAndDefaultSettings', () => {
describe('blocks', () => {
it('should report invalid keys within preset settings', async () => {
const theme: MockTheme = {
'blocks/_tab.liquid': horizonNestedBlockSchema,
'blocks/tabs.liquid': createHorizonBlockSchema(invalidBlockPresetSchema, null),
};

const offenses = await check(theme, [ValidPresetAndDefaultSettings]);
expect(offenses).to.have.length(4);
expect(offenses[0].message).to.include(
'Preset setting "invalid_setting" does not exist in settings',
);
expect(offenses[1].message).to.include(
'Preset block setting "invalid_nested_setting" does not exist in settings',
);
expect(offenses[2].message).to.include(
'Preset block setting "invalid_nested_setting_1" does not exist in settings',
);
expect(offenses[3].message).to.include(
'Preset block setting "invalid_nested_setting_2" does not exist in settings',
);
});

it('should report invalid keys within default settings', async () => {
const theme: MockTheme = {
'blocks/_tab.liquid': horizonNestedBlockSchema,
'blocks/tabs.liquid': createHorizonBlockSchema(null, invalidBlockDefaultSchema),
};

const offenses = await check(theme, [ValidPresetAndDefaultSettings]);
expect(offenses).to.have.length(3);
expect(offenses[0].message).to.include(
'Default setting "invalid_setting" does not exist in settings',
);
expect(offenses[1].message).to.include(
'Default block setting "invalid_nested_setting_1" does not exist in settings',
);
expect(offenses[2].message).to.include(
'Default block setting "invalid_nested_setting_2" does not exist in settings',
);
});

it('should not report when all preset settings in the block are valid', async () => {
const theme: MockTheme = {
'blocks/_tab.liquid': horizonNestedBlockSchema,
'blocks/tabs.liquid': validHorizonPresetSchema,
};

const offenses = await check(theme, [ValidPresetAndDefaultSettings]);
expect(offenses).to.have.length(0);
});

it('should not report when all default settings in the block are valid', async () => {
const theme: MockTheme = {
'blocks/_tab.liquid': horizonNestedBlockSchema,
'blocks/tabs.liquid': validHorizonDefaultSchema,
};

const offenses = await check(theme, [ValidPresetAndDefaultSettings]);
expect(offenses).to.have.length(0);
});
});

describe('sections', () => {
it('should report invalid keys within preset setting', async () => {
const theme: MockTheme = {
'blocks/_tab.liquid': horizonNestedBlockSchema,
'blocks/announcement.liquid': horizonNestedBlockSchema,
'sections/announcement-bar.liquid': createDawnSectionSchema(
invalidSectionPresetSchema,
null,
),
};

const offenses = await check(theme, [ValidPresetAndDefaultSettings]);
expect(offenses).to.have.length(4);
expect(offenses[0].message).to.include(
`Preset setting "invalid_setting" does not exist in settings`,
);
expect(offenses[1].message).to.include(
`Preset block setting "invalid_nested_setting_1" does not exist in settings`,
);
expect(offenses[2].message).to.include(
`Preset block setting "invalid_nested_setting_3" does not exist in settings`,
);
expect(offenses[3].message).to.include(
`Preset block setting "invalid_nested_setting_2" does not exist in settings`,
);
});

it('should report invalid keys in a sections default setting', async () => {
const theme: MockTheme = {
'blocks/_tab.liquid': horizonNestedBlockSchema,
'sections/announcement-bar.liquid': createDawnSectionSchema(
null,
invalidSectionDefaultSchema,
),
};

const offenses = await check(theme, [ValidPresetAndDefaultSettings]);
expect(offenses).to.have.length(5);
expect(offenses[0].message).to.include(
`Default setting "invalid_setting" does not exist in settings`,
);
expect(offenses[1].message).to.include(
`Default block setting "invalid_nested_setting_1" does not exist in settings`,
);
expect(offenses[2].message).to.include(
`Default block setting "invalid_nested_setting_2" does not exist in settings`,
);
expect(offenses[3].message).to.include(
`Default block setting "invalid_nested_setting_3" does not exist in settings`,
);
expect(offenses[4].message).to.include(
`Default block setting "invalid_nested_setting_4" does not exist in settings`,
);
});

it('should not report when all preset settings are valid', async () => {
const theme: MockTheme = {
'sections/announcement-bar.liquid': validDawnPresetSchema,
};

const offenses = await check(theme, [ValidPresetAndDefaultSettings]);
expect(offenses).to.have.length(0);
});

it('should not report when all default settings are valid', async () => {
const theme: MockTheme = {
'sections/announcement-bar.liquid': validDawnDefaultSchema,
};

const offenses = await check(theme, [ValidPresetAndDefaultSettings]);
expect(offenses).to.have.length(0);
});
});
});
Loading
Loading