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

Validate saved data #591

Merged
merged 39 commits into from
Jan 12, 2019
Merged

Validate saved data #591

merged 39 commits into from
Jan 12, 2019

Conversation

PolinaShneider
Copy link
Contributor

@PolinaShneider PolinaShneider commented Jan 9, 2019

Added plugins data validation
image

*/
public validateData(data: BlockToolData): BlockToolData|false {
public async validateData(data: BlockToolData): Promise<BlockToolData|false> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лучше назвать просто validate, как остальные методы

src/components/modules/saver.ts Outdated Show resolved Hide resolved
blocks.map(async (block: Block, index) => {
const validData = await block.validateData(sanitizedData[index].data);
if (!validData) {
console.warn(`Invalid data in ${sanitizedData[index].tool} Tool!`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • не нужен варнинг
  • текст лога я скидывал
  • для логирования есть специальный метод

*/
public validateData(data: BlockToolData): BlockToolData|false {
public async validateData(data: BlockToolData): Promise<BlockToolData|false> {
Copy link
Member

@khaydarov khaydarov Jan 10, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Что вернёт функция? blocktooldata либо всегда false?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blocktooldata, если валидация пройдена. Иначе false

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Поменяй тогда и в описании плиз, а то там boolean

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А True тоже булев тип

blocks.map(async (block: Block, index) => {
const validData = await block.validate(sanitizedData[index].data);
if (!validData) {
_.log(`Block «${sanitizedData[index].tool}» skipped because saved data is invalid`, 'warn');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

не нужен варнинг

@@ -40,6 +41,16 @@ export default class Saver extends Module {
const extractedData = await Promise.all(chainData);
const sanitizedData = await Sanitizer.sanitizeBlocks(extractedData);

await Promise.all(
blocks.map(async (block: Block, index) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

выше уже есть цикл по блокам, можно просто не пушить невалидные данные

src/components/block.ts Show resolved Hide resolved
blocks.forEach((block: Block) => {
chainData.push(block.save());
});
await Promise.all(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

зачем тут await? и Promise.all?

src/components/modules/saver.ts Show resolved Hide resolved
Copy link
Member

@talyguryn talyguryn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

работает

*/
public validateData(data: BlockToolData): BlockToolData|false {
public validate(data: BlockToolData): boolean {
let isValid = true;

if (this.tool.validate instanceof Function) {
isValid = this.tool.validate(data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо бы сделать вызов с await на случай, если плагин будет иметь асинхронный validate.

docs/tools.md Outdated
@@ -41,6 +41,8 @@ Process Tool's element created by `render()` function in DOM and return Block's

### validate() _optional_

Allows to check correctness of Tool's `data`. If Tool's data didn't pass the validation it won't be saved.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Стоит описать тогда аргументы и тип возвращаемого значения

@@ -34,10 +35,21 @@ export default class Saver extends Module {
ModificationsObserver.disable();

blocks.forEach((block: Block) => {
chainData.push(block.save());
chainData.push(block.save().then(async (blockData) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Может лучше тогда все на then переписать. И то, и другое как-то не очень смотрится

* @param {Block} block - Editor's Tool
*/
private pushValidData(block: Block): Promise<void|{isValid: boolean}> {
return Promise.resolve().then(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это не нужно, сделай сам метод async

* Calls block's save method and pushes only valid data to blocks array
* @param {Block} block - Editor's Tool
*/
private pushValidData(block: Block): Promise<void|{isValid: boolean}> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Надо бы как-то по-другому метод назвать

gohabereg
gohabereg previously approved these changes Jan 12, 2019
@gohabereg gohabereg dismissed their stale review January 12, 2019 02:17

Not working

}
});

extractedData.filter((blockData) => blockData.isValid);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

это не рабочий код

src/components/modules/saver.ts Outdated Show resolved Hide resolved

return {...blockData, isValid};
}

/**
* Creates output object with saved data, time and version of editor
* @param {Object} allExtractedData
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

надо описать тип

@PolinaShneider PolinaShneider merged commit fc3e146 into master Jan 12, 2019
@PolinaShneider PolinaShneider deleted the validate-saved-data branch January 12, 2019 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants