Skip to content

Commit

Permalink
feat: create schemas (#41)
Browse files Browse the repository at this point in the history
* feat: create shemas

* feat: refactor schemas usage after review

* feat: update import paths
  • Loading branch information
NikitaCG authored May 15, 2023
1 parent 5643d98 commit f4f28f3
Show file tree
Hide file tree
Showing 20 changed files with 391 additions and 4 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
"@commitlint/config-conventional": "^17.4.3",
"@doc-tools/transform": "^2.9.0",
"@gravity-ui/eslint-config": "^2.0.0",
"@gravity-ui/page-constructor": "^2.6.0",
"@gravity-ui/page-constructor": "^2.13.0",
"@gravity-ui/prettier-config": "^1.0.1",
"@gravity-ui/stylelint-config": "^1.0.0",
"@gravity-ui/tsconfig": "^1.0.0",
Expand Down
23 changes: 23 additions & 0 deletions src/blocks/Author/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {validators} from '@gravity-ui/page-constructor';

const {
common: {BlockBaseProps},
} = validators;

import {BlockType} from '../../models/common';
import {BlogBlockBase} from '../../schema/common';

export const Author = {
[BlockType.Author]: {
type: 'object',
additionalProperties: false,
required: ['authorId'],
properties: {
...BlockBaseProps,
...BlogBlockBase,
authorId: {
type: 'number',
},
},
},
};
32 changes: 32 additions & 0 deletions src/blocks/Banner/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {validators} from '@gravity-ui/page-constructor';

import {BlockType} from '../../models/common';
import {BlogBlockBase} from '../../schema/common';

const {
subBlocks: {ContentBase},
common: {BlockBaseProps},
} = validators;

export const Banner = {
[BlockType.Banner]: {
type: 'object',
additionalProperties: false,
required: ['title', 'text', 'image'],
properties: {
...BlockBaseProps,
...BlogBlockBase,
...ContentBase,
color: {
type: 'string',
},
image: {
type: 'string',
},
imageSize: {
type: 'string',
enum: ['s', 'm'],
},
},
},
};
32 changes: 32 additions & 0 deletions src/blocks/CTA/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {validators} from '@gravity-ui/page-constructor';

import {BlockType} from '../../models/common';
import {BlogBlockBase} from '../../schema/common';

const {
subBlocks: {ContentBase},
common: {BlockBaseProps},
} = validators;

export const CTA = {
[BlockType.CTA]: {
type: 'object',
additionalProperties: false,
required: ['items'],
properties: {
...BlockBaseProps,
...BlogBlockBase,
items: {
type: 'array',
items: {
type: 'object',
additionalProperties: false,
required: ['title', 'links'],
properties: {
...ContentBase,
},
},
},
},
},
};
39 changes: 39 additions & 0 deletions src/blocks/ColoredText/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {validators} from '@gravity-ui/page-constructor';

import {BlockType} from '../../models/common';
import {BlogBlockBase} from '../../schema/common';

const {
subBlocks: {ContentBase},
components: {ImageProps},
common: {BlockBaseProps},
} = validators;

const BackgroundProps = {
type: 'object',
additionalProperties: false,
properties: {
image: ImageProps,
color: {
type: 'string',
},
altText: {
type: 'string',
contentType: 'text',
},
},
};

export const ColoredText = {
[BlockType.ColoredText]: {
type: 'object',
additionalProperties: false,
required: ['text'],
properties: {
...BlockBaseProps,
...BlogBlockBase,
...ContentBase,
background: BackgroundProps,
},
},
};
29 changes: 29 additions & 0 deletions src/blocks/Feed/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {validators} from '@gravity-ui/page-constructor';

const {
common: {TitleProps, BlockBaseProps},
} = validators;

import {BlockType} from '../../models/common';

export const Feed = {
[BlockType.Feed]: {
additionalProperties: false,
required: [],
properties: {
...BlockBaseProps,
title: TitleProps,
image: {
type: 'string',
},
description: {
type: 'string',
contentType: 'text',
},
size: {
type: 'string',
enum: ['s', 'm'],
},
},
},
};
21 changes: 21 additions & 0 deletions src/blocks/Header/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {validators} from '@gravity-ui/page-constructor';

import {BlockType} from '../../models/common';
import {BlogBlockBase} from '../../schema/common';

const {
blocks: {HeaderProperties},
common: {BlockBaseProps},
} = validators;

export const Header = {
[BlockType.Header]: {
type: 'object',
additionalProperties: false,
properties: {
...BlockBaseProps,
...BlogBlockBase,
...HeaderProperties,
},
},
};
25 changes: 25 additions & 0 deletions src/blocks/Layout/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {validators} from '@gravity-ui/page-constructor';

const {
common: {BlockBaseProps, ChildrenProps},
} = validators;

import {BlockType} from '../../models/common';
import {BlogBlockBase} from '../../schema/common';

export const Layout = {
[BlockType.Layout]: {
type: 'object',
additionalProperties: false,
required: ['children'],
properties: {
...BlockBaseProps,
...BlogBlockBase,
children: ChildrenProps,
mobileOrder: {
type: 'string',
enum: ['reverse', 'straight'],
},
},
},
};
24 changes: 24 additions & 0 deletions src/blocks/Media/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {validators} from '@gravity-ui/page-constructor';

import {BlockType} from '../../models/common';
import {BlogBlockBase} from '../../schema/common';

const {
common: {BlockBaseProps, MediaProps},
} = validators;

export const Media = {
[BlockType.Media]: {
type: 'object',
additionalProperties: false,
properties: {
...BlockBaseProps,
...BlogBlockBase,
...MediaProps,
text: {
type: 'string',
contentType: 'text',
},
},
},
};
19 changes: 19 additions & 0 deletions src/blocks/Meta/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {validators} from '@gravity-ui/page-constructor';

const {
common: {BlockBaseProps},
} = validators;

import {BlockType} from '../../models/common';
import {BlogBlockBase} from '../../schema/common';

export const Meta = {
[BlockType.Meta]: {
type: 'object',
additionalProperties: false,
properties: {
...BlockBaseProps,
...BlogBlockBase,
},
},
};
19 changes: 19 additions & 0 deletions src/blocks/Suggest/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import {validators} from '@gravity-ui/page-constructor';

const {
common: {BlockBaseProps},
} = validators;

import {BlockType} from '../../models/common';
import {BlogBlockBase} from '../../schema/common';

export const Suggest = {
[BlockType.Suggest]: {
type: 'object',
additionalProperties: false,
properties: {
...BlockBaseProps,
...BlogBlockBase,
},
},
};
24 changes: 24 additions & 0 deletions src/blocks/YFM/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {validators} from '@gravity-ui/page-constructor';

const {
common: {BlockBaseProps},
} = validators;

import {BlockType} from '../../models/common';
import {BlogBlockBase} from '../../schema/common';

export const YFM = {
[BlockType.YFM]: {
type: 'object',
additionalProperties: false,
required: ['text'],
properties: {
...BlockBaseProps,
...BlogBlockBase,
text: {
type: 'string',
contentType: 'yfm',
},
},
},
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ export {BlogPage} from './containers/BlogPage/BlogPage';
export * from './models/common';
export * from './models/locale';

export * from './schema';

export {BREAKPOINTS} from './constants';
5 changes: 5 additions & 0 deletions src/schema/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Shemas and validators

#### We use [`Ajv JSON schema validator`](https://ajv.js.org/) for validation configs with blocks descriptions.

#### You can use blocks schemas in your backoffice or everything else places where you check configs.
10 changes: 10 additions & 0 deletions src/schema/blocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export * from '../blocks/Author/schema';
export * from '../blocks/Banner/schema';
export * from '../blocks/ColoredText/schema';
export * from '../blocks/CTA/schema';
export * from '../blocks/Feed/schema';
export * from '../blocks/Layout/schema';
export * from '../blocks/Media/schema';
export * from '../blocks/Meta/schema';
export * from '../blocks/Suggest/schema';
export * from '../blocks/YFM/schema';
34 changes: 34 additions & 0 deletions src/schema/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
export interface ObjectSchema extends Record<string, unknown> {
properties: object;
}

const sizeTypes = ['xxs', 'xs', 's', 'm', 'l', 'xl', 'xxl'];

export const filteredItem = (itemsSchema: ObjectSchema) => ({
...itemsSchema,
type: 'object',
properties: {
when: {
type: 'string',
},
...itemsSchema.properties,
},
});

export const BlogBlockBase = {
paddingTop: {
type: 'string',
enum: sizeTypes,
},
paddingBottom: {
type: 'string',
enum: sizeTypes,
},
fullWidth: {
type: 'boolean',
},
column: {
type: 'string',
enum: ['left', 'right'],
},
};
1 change: 1 addition & 0 deletions src/schema/headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '../blocks/Header/schema';
Loading

0 comments on commit f4f28f3

Please sign in to comment.