Skip to content

Commit a6c8dec

Browse files
author
Juli Ovechkina
authored
feat(Navigation): add schema validation (#120)
1 parent 2dfee78 commit a6c8dec

File tree

6 files changed

+114
-5
lines changed

6 files changed

+114
-5
lines changed

src/containers/PageConstructor/__stories__/data.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,25 +188,31 @@
188188
]
189189
},
190190
{
191+
"type": "link",
191192
"text": "Link with spaces",
192193
"url": "https://example.com",
193194
"icon": "https://storage.yandexcloud.net/cloud-www-assets/constructor/storybook/images/icon_3_light.svg"
194195
},
195196
{
196-
"text": "Link2",
197-
"url": "https://example.com"
197+
"type": "link",
198+
"text": "Link with arrow",
199+
"url": "https://example.com",
200+
"arrow": true
198201
},
199202
{
203+
"type": "link",
200204
"text": "Link3",
201205
"url": "https://example.com"
202206
},
203207
{
208+
"type": "link",
204209
"text": "Link4",
205210
"url": "https://example.com"
206211
}
207212
],
208213
"rightItems": [
209214
{
215+
"type": "link",
210216
"text": "Link",
211217
"url": "https://example.com",
212218
"icon": "https://storage.yandexcloud.net/cloud-www-assets/constructor/storybook/images/icon_2_light.svg"

src/models/navigation.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface NavigationItemBase {
1414
url?: string;
1515
}
1616

17-
export interface NavigationLinkItem extends NavigationItemBase {
17+
export interface NavigationLinkItem extends Omit<NavigationItemBase, 'url'> {
1818
type: NavigationItemType.Link;
1919
url: string;
2020
arrow?: boolean;
@@ -23,8 +23,6 @@ export interface NavigationLinkItem extends NavigationItemBase {
2323

2424
export interface NavigationButtonItem extends ButtonProps {
2525
type: NavigationItemType.Button;
26-
url: string;
27-
target?: string;
2826
}
2927

3028
export interface NavigationDropdownItem extends NavigationItemBase {

src/navigation/schema.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import {ButtonProps} from '../schema/validators/common';
2+
import {ImageProps, urlPattern} from '../components/Image/schema';
3+
import {omit} from 'lodash';
4+
import {filteredArray} from '../schema/validators/utils';
5+
6+
const NavigationItemType = {
7+
type: 'string',
8+
enum: ['link', 'button', 'dropdown'],
9+
};
10+
11+
export const LogoProps = {
12+
type: 'object',
13+
additionalProperties: false,
14+
required: ['icon'],
15+
properties: {
16+
icon: ImageProps,
17+
text: {
18+
type: 'string',
19+
contentType: 'text',
20+
},
21+
url: {
22+
type: 'string',
23+
},
24+
},
25+
};
26+
27+
const NavigationItemBaseProps = {
28+
text: {
29+
type: 'string',
30+
contentType: 'text',
31+
},
32+
url: {
33+
type: 'string',
34+
},
35+
icon: {
36+
type: 'string',
37+
pattern: urlPattern,
38+
},
39+
};
40+
41+
const NavigationItemBaseLinkProps = omit(NavigationItemBaseProps, ['url']);
42+
43+
const NavigationLinkItemProps = {
44+
type: 'object',
45+
additionalProperties: false,
46+
required: ['type', 'text'],
47+
properties: {
48+
...NavigationItemBaseLinkProps,
49+
type: {...NavigationItemType},
50+
url: {
51+
type: 'string',
52+
},
53+
target: {
54+
type: 'string',
55+
},
56+
arrow: {
57+
type: 'boolean',
58+
},
59+
},
60+
};
61+
62+
const NavigationButtonItemProps = {
63+
type: 'object',
64+
additionalProperties: false,
65+
required: ['type', 'text', 'url'],
66+
properties: {
67+
...ButtonProps,
68+
type: {...NavigationItemType},
69+
},
70+
};
71+
72+
const NavigationDropdownItemProps = {
73+
type: 'object',
74+
additionalProperties: false,
75+
required: ['type', 'items'],
76+
properties: {
77+
...NavigationItemBaseProps,
78+
type: {...NavigationItemType},
79+
items: filteredArray(NavigationLinkItemProps),
80+
},
81+
};
82+
83+
const NavigationItemProps = {
84+
oneOf: [
85+
filteredArray(NavigationLinkItemProps),
86+
filteredArray(NavigationButtonItemProps),
87+
filteredArray(NavigationDropdownItemProps),
88+
],
89+
};
90+
91+
export const NavigationHeaderProps = {
92+
type: 'object',
93+
additionalProperties: false,
94+
required: ['leftItems'],
95+
properties: {
96+
leftItems: NavigationItemProps,
97+
rightItems: NavigationItemProps,
98+
},
99+
};

src/schema/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import {
4040

4141
import {AnimatableProps, BackgroundProps, MenuProps, withTheme} from './validators/common';
4242
import {filteredItem} from './validators/utils';
43+
import {LogoProps, NavigationHeaderProps} from './validators/navigation';
4344

4445
export type SchemaBlock = object;
4546

@@ -185,6 +186,8 @@ export function generateDefaultSchema(config?: SchemaCustomConfig) {
185186
required: ['blocks'],
186187
properties: {
187188
...AnimatableProps,
189+
logo: withTheme(LogoProps),
190+
header: NavigationHeaderProps,
188191
blocks: {
189192
type: 'array',
190193
items: {

src/schema/validators/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import * as common from './common';
33
import * as subBlocks from './sub-blocks';
44
import * as pixel from './pixel';
55
import * as utils from './utils';
6+
import * as navigation from './navigation';
67

78
export const validators = {
89
blocks,
910
common,
1011
subBlocks,
1112
pixel,
1213
utils,
14+
navigation,
1315
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from '../../navigation/schema';

0 commit comments

Comments
 (0)