Skip to content

Commit

Permalink
feat: tree-shakeable ES imports for TS form validators (#9374)
Browse files Browse the repository at this point in the history
- Use tree-shakeable ES imports for TS form validators based on the latest version of validators lib.
- Add missing `return` in the `submit()` method
  • Loading branch information
haijian-vaadin authored and pleku committed Dec 7, 2020
1 parent 4021897 commit 02c98b4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 20 deletions.
4 changes: 2 additions & 2 deletions flow-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"dependencies": {
"lit-html": "^1.2.1",
"@types/validator": "10.11.3",
"validator": "12.0.0"
"@types/validator": "13.1.0",
"validator": "13.1.17"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ export class Binder<T, M extends AbstractModel<T>> extends BinderNode<T, M> {
* It's a no-op if the onSubmit callback is undefined.
*/
async submit(): Promise<T|void>{
if(this[_onSubmit]!==undefined){
this.submitTo(this[_onSubmit]);
if (this[_onSubmit] !== undefined) {
return this.submitTo(this[_onSubmit]);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
/* tslint:disable:max-classes-per-file */

import * as isAfter from 'validator/lib/isAfter';
import * as isBefore from 'validator/lib/isBefore';
import * as isBoolean from 'validator/lib/isBoolean';
import * as isDecimal from 'validator/lib/isDecimal';
import * as isEmail from 'validator/lib/isEmail';
// @ts-ignore (vlukashov: have not investigated why, but for the `isFloat` module the d.ts file is not accurate)
import {default as isFloat} from 'validator/lib/isFloat';
import * as isLength from 'validator/lib/isLength';
import * as isNumeric from 'validator/lib/isNumeric';
import * as matches from 'validator/lib/matches';
import * as toFloat from 'validator/lib/toFloat';
import isAfter from 'validator/es/lib/isAfter';
import isBefore from 'validator/es/lib/isBefore';
import isBoolean from 'validator/es/lib/isBoolean';
import isDecimal from 'validator/es/lib/isDecimal';
import isEmail from 'validator/es/lib/isEmail';
import isFloat from 'validator/es/lib/isFloat';
import isLength from 'validator/es/lib/isLength';
import isNumeric from 'validator/es/lib/isNumeric';
import matches from 'validator/es/lib/matches';
import toFloat from 'validator/es/lib/toFloat';
import { Validator } from './Validation';

interface ValidatorAttributes {
Expand Down Expand Up @@ -57,7 +56,7 @@ export class Required<T> extends AbstractValidator<T> {
}
}

function _asValidatorAttributes(attrs: ValueNumberAttributes | number | string | PatternAttributes | string | RegExp) {
function _asValidatorAttributes(attrs: ValueNumberAttributes | number | string | PatternAttributes | RegExp) {
return typeof attrs === 'object' ? attrs : {};
}

Expand Down Expand Up @@ -219,7 +218,7 @@ export class Size extends AbstractValidator<string> {
if (this.min && this.min > 0 && !new Required().validate(value)) {
return false;
}
return isLength(value, this.min, this.max);
return isLength(value, { min: this.min, max: this.max });
}
}

Expand Down
8 changes: 7 additions & 1 deletion flow-client/src/test/frontend/form/ValidationTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,20 @@ suite("form/Validation", () => {

suite('submitTo', () => {
test("should be able to call submit() if onSubmit is pre configured", async () => {
const binder = new Binder(view, OrderModel, {
const binder = new Binder(view, TestModel, {
onSubmit: async () => {}
});
const binderSubmitToSpy = sinon.spy(binder, 'submitTo');
await binder.submit();
sinon.assert.calledOnce(binderSubmitToSpy);
});

test("should return the result of the endpoint call when calling submit()", async () => {
const binder = new Binder(view, TestModel, {onSubmit: async (testEntity) => testEntity});
const result = await binder.submit();
assert.deepEqual(result, binder.value);
})

test("should throw on validation failure", async () => {
try {
await binder.submitTo(async() => {});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ static Map<String, String> getDefaultDevDependencies() {
defaults.put("lit-css-loader", "0.0.4");
defaults.put("lit-element", "2.3.1");
defaults.put("lit-html", "1.2.1");
defaults.put("@types/validator", "10.11.3");
defaults.put("validator", "12.0.0");
defaults.put("@types/validator", "13.1.0");
defaults.put("validator", "13.1.17");

// Forcing chokidar version for now until new babel version is available
// check out https://github.com/babel/babel/issues/11488
Expand Down

0 comments on commit 02c98b4

Please sign in to comment.