-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Validate saved data #591
Conversation
src/components/block.ts
Outdated
*/ | ||
public validateData(data: BlockToolData): BlockToolData|false { | ||
public async validateData(data: BlockToolData): Promise<BlockToolData|false> { |
There was a problem hiding this comment.
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
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!`); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- не нужен варнинг
- текст лога я скидывал
- для логирования есть специальный метод
src/components/block.ts
Outdated
*/ | ||
public validateData(data: BlockToolData): BlockToolData|false { | ||
public async validateData(data: BlockToolData): Promise<BlockToolData|false> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Что вернёт функция? blocktooldata либо всегда false?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
blocktooldata, если валидация пройдена. Иначе false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Поменяй тогда и в описании плиз, а то там boolean
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
А True тоже булев тип
src/components/modules/saver.ts
Outdated
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'); |
There was a problem hiding this comment.
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
@@ -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) => { |
There was a problem hiding this comment.
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
blocks.forEach((block: Block) => { | ||
chainData.push(block.save()); | ||
}); | ||
await Promise.all( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
зачем тут await? и Promise.all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
работает
src/components/block.ts
Outdated
*/ | ||
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); |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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
@@ -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) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Может лучше тогда все на then переписать. И то, и другое как-то не очень смотрится
src/components/modules/saver.ts
Outdated
* @param {Block} block - Editor's Tool | ||
*/ | ||
private pushValidData(block: Block): Promise<void|{isValid: boolean}> { | ||
return Promise.resolve().then( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Это не нужно, сделай сам метод async
src/components/modules/saver.ts
Outdated
* 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}> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Надо бы как-то по-другому метод назвать
…nto validate-saved-data
…nto validate-saved-data
src/components/modules/saver.ts
Outdated
} | ||
}); | ||
|
||
extractedData.filter((blockData) => blockData.isValid); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
это не рабочий код
…nto validate-saved-data
src/components/modules/saver.ts
Outdated
|
||
return {...blockData, isValid}; | ||
} | ||
|
||
/** | ||
* Creates output object with saved data, time and version of editor | ||
* @param {Object} allExtractedData |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
надо описать тип
Added plugins data validation
![image](https://user-images.githubusercontent.com/15448200/50970734-49d7d200-14f3-11e9-9b81-797c96a06c67.png)