Skip to content
This repository has been archived by the owner on Apr 24, 2023. It is now read-only.

Improve validation with asynchronous pipeline #175

Open
wants to merge 22 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
1aefc98
Fix: check whether validator is a function to avoid type error
Mosoc Apr 27, 2019
db82657
Fix: typo `ComponentWithValidation`
Mosoc Apr 27, 2019
8c03926
Only pass schema property to avj.compile()
Mosoc Apr 29, 2019
ae5d0ce
Fix some coding style
Mosoc Apr 29, 2019
991c04f
Add async keyword to validate function
Mosoc May 9, 2019
0d51466
Update validation test file because of object structure change
Mosoc May 14, 2019
ff1312c
Update validator test
Mosoc May 14, 2019
2f642e0
Implement asynchronous pipline of validation
Mosoc May 9, 2019
763fde4
Fix typo: withValidation
Mosoc May 14, 2019
dfa6ce1
Reform validation's test file with async/await, and remove unused props
Mosoc May 15, 2019
c5711d0
Change the test label of customized validator
Mosoc May 15, 2019
9e5748a
Create test cases of sync validator function
Mosoc May 15, 2019
1c731c9
Correct fault in validation HoC
Mosoc May 15, 2019
e10c57f
Create test cases with async-wait functions
Mosoc May 15, 2019
67ffc9a
Create test cases with promise operations
Mosoc May 15, 2019
a493e5a
Add try-catch in validate to handle uncaught error
Mosoc May 15, 2019
096a86a
Workaround for when validator is not a function
Mosoc May 15, 2019
91038ba
Reove `_ ` prefix of promise creator
Mosoc May 16, 2019
831fd24
Update type definition of validation
Mosoc May 16, 2019
471a875
Update the usage of customized validator in docs
Mosoc May 18, 2019
0993e84
Use keep (verb) as prefix for promise creator
Mosoc May 19, 2019
9157f2c
Change `keep`(verb) with `promise`(verb)
Mosoc May 29, 2019
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
Prev Previous commit
Next Next commit
Workaround for when validator is not a function
  • Loading branch information
Mosoc committed May 15, 2019
commit 096a86a374112b1ca08ac96c800764467024b73e
14 changes: 11 additions & 3 deletions packages/canner/src/hocs/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,13 @@ export default function withValidation(Com: React.ComponentType<*>) {
if(value && checkSchema(schema)) {
promiseQueue.push(_schemaValidation(schema, errorMessage)(value));
}
if(checkValidator(validator)) {
promiseQueue.push(_customizedValidator(validator)(value));
}
if(validator) {
if(checkValidator(validator)) {
promiseQueue.push(_customizedValidator(validator)(value));
} else {
throw 'Validator should be a function'
}
}
}

const ValidationResult = await Promise.all(promiseQueue);
Expand All @@ -144,6 +148,10 @@ export default function withValidation(Com: React.ComponentType<*>) {
}
}
catch(err){
this.setState({
error: true,
errorInfo: [].concat({message: toString(err)})
});
return {
...result,
error: true,
Expand Down
23 changes: 23 additions & 0 deletions packages/canner/test/hocs/validation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,29 @@ describe('withValidation', () => {
})
});

it('validator is not a function', async () => {
const result = {
data: {
0: { url: ''}
}
};
const wrapper = mount(<WrapperComponent
{...props}
validation={
{
validator: 'validator'
}
}
/>);
await wrapper.instance().validate(result)
expect(wrapper.state()).toMatchObject({
error: true,
errorInfo: [{
message: 'Validator should be a function'
}]
})
});

// Async-await functions
it('should use customized async validator with error message', async () => {
const result = {
Expand Down