Skip to content

Commit f598ae0

Browse files
simplr-validation webpack building (#45)
2 parents eb32324 + bd77636 commit f598ae0

File tree

5 files changed

+85
-33
lines changed

5 files changed

+85
-33
lines changed

packages/simplr-validation/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "simplr-validation",
3-
"version": "4.0.0-pre-alpha.8",
3+
"version": "4.0.1-alpha",
44
"description": "Validation library for simplr-forms.",
55
"repository": "SimplrJS/simplr-forms",
66
"homepage": "https://github.com/SimplrJS/simplr-forms",
7-
"main": "dist/simplr-validation.js",
8-
"types": "@types/index.d.ts",
7+
"main": "index.js",
8+
"types": "index.d.ts",
99
"author": "simplrjs <[email protected]> (https://github.com/simplrjs)",
1010
"scripts": {
1111
"test": "jest",
@@ -56,7 +56,7 @@
5656
"action-emitter": "^0.2.1",
5757
"immutable": "^3.8.1",
5858
"react": "^15.5.4",
59-
"simplr-forms": "4.0.0-pre-alpha.20",
59+
"simplr-forms": "4.0.1-alpha",
6060
"validator": "^7.0.0"
6161
},
6262
"jest": {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export * from "./abstractions";
1+
export * from "./abstractions/index";
Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,53 @@
11
import { FieldValue } from "simplr-forms/contracts";
2-
import { ProcessValue } from "simplr-forms/utils";
2+
import {
3+
ProcessValue,
4+
IsComponentOfType,
5+
RenderComponents,
6+
ConstructFormError
7+
} from "simplr-forms/utils";
38

4-
import { Validator, FIELD_VALIDATOR_FUNCTION_NAME, FORM_VALIDATOR_FUNCTION_NAME } from "../contracts";
9+
import {
10+
Validator,
11+
FIELD_VALIDATOR_FUNCTION_NAME,
12+
FORM_VALIDATOR_FUNCTION_NAME
13+
} from "../contracts";
514

615

16+
function IsPromise<T>(value: any): value is Promise<T> {
17+
return value != null && value.then != null && value.catch != null;
18+
}
19+
20+
export async function ValidateValue(
21+
components: Array<JSX.Element>,
22+
value: any,
23+
validatorTypeFunctionName: string) {
24+
const validators = components.filter(x => IsComponentOfType(x, validatorTypeFunctionName));
25+
const renderedValidators = RenderComponents<Validator>(validators);
26+
27+
for (const validator of renderedValidators) {
28+
const validationResult = validator.Validate(value);
29+
// Ensure that we have a promise
30+
let promise: Promise<void>;
31+
if (IsPromise<void>(validationResult)) {
32+
promise = validationResult;
33+
} else {
34+
promise = new Promise<void>((resolve, reject) => {
35+
const error = ConstructFormError(validationResult);
36+
if (error !== undefined) {
37+
reject(validationResult);
38+
return;
39+
}
40+
resolve();
41+
});
42+
}
43+
await promise;
44+
}
45+
}
46+
747
export function ValidateField(components: Array<JSX.Element>, value: FieldValue) {
8-
return ProcessValue<Validator, Promise<void>>(components, value, FIELD_VALIDATOR_FUNCTION_NAME,
9-
async (processor, value) => {
10-
const validationResult = processor.Validate(value);
11-
if (validationResult != null && typeof validationResult === "string") {
12-
throw validationResult;
13-
}
14-
return await validationResult;
15-
});
48+
return ValidateValue(components, value, FIELD_VALIDATOR_FUNCTION_NAME);
1649
}
1750

18-
export function ValidateForm(components: Array<JSX.Element>, value: FieldValue) {
19-
return ProcessValue<Validator, Promise<void>>(components, value, FORM_VALIDATOR_FUNCTION_NAME,
20-
async (processor, value) => {
21-
const validationResult = processor.Validate(value);
22-
if (validationResult != null && typeof validationResult === "string") {
23-
throw validationResult;
24-
}
25-
return await validationResult;
26-
});
51+
export function ValidateForm(components: Array<JSX.Element>, value: any) {
52+
return ValidateValue(components, value, FORM_VALIDATOR_FUNCTION_NAME);
2753
}

packages/simplr-validation/tools/webpack.config.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ const packageJson = require("./package.json");
55

66
let externals: {
77
[key: string]: any
8-
} = {};
8+
} = {
9+
"simplr-forms/utils": "simplr-forms/utils",
10+
"simplr-forms/actions": "simplr-forms/actions",
11+
"simplr-forms/contracts": "simplr-forms/contracts",
12+
"simplr-forms/stores": "simplr-forms/stores"
13+
};
914

1015
for (const key in packageJson.dependencies) {
1116
if (packageJson.dependencies.hasOwnProperty(key)) {
@@ -36,7 +41,15 @@ const externalsResolver = [
3641

3742
if (passingTest != null) {
3843
const resolvedPath = path.resolve(context, request);
44+
let notIndexFile = true;
45+
for (const directory of directoriesToTest) {
46+
if (request === `./${directory}/index`) {
47+
notIndexFile = false;
48+
}
49+
}
50+
3951
const shouldReplaceWithCustomResolve =
52+
notIndexFile &&
4053
request.indexOf("src") === -1 &&
4154
resolvedPath.indexOf(path.join(__dirname, `src/${passingTest.directory}`)) !== -1;
4255

@@ -53,9 +66,9 @@ const externalsResolver = [
5366
module.exports = {
5467
entry: {
5568
index: "./src/index.ts",
56-
abstractions: "./src/abstractions.ts",
57-
subscribers: "./src/subscribers.ts",
58-
utils: "./src/utils.ts"
69+
abstractions: "./src/abstractions/index.ts",
70+
subscribers: "./src/subscribers/index.ts",
71+
utils: "./src/utils/index.ts"
5972
},
6073
output: {
6174
filename: "./dist/[name].js",

packages/simplr-validation/webpack.config.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22
Object.defineProperty(exports, "__esModule", { value: true });
33
var path = require("path");
44
var packageJson = require("./package.json");
5-
var externals = {};
5+
var externals = {
6+
"simplr-forms/utils": "simplr-forms/utils",
7+
"simplr-forms/actions": "simplr-forms/actions",
8+
"simplr-forms/contracts": "simplr-forms/contracts",
9+
"simplr-forms/stores": "simplr-forms/stores"
10+
};
611
for (var key in packageJson.dependencies) {
712
if (packageJson.dependencies.hasOwnProperty(key)) {
813
externals[key] = key;
@@ -29,7 +34,15 @@ var externalsResolver = [
2934
}
3035
if (passingTest != null) {
3136
var resolvedPath = path.resolve(context, request);
32-
var shouldReplaceWithCustomResolve = request.indexOf("src") === -1 &&
37+
var notIndexFile = true;
38+
for (var _a = 0, directoriesToTest_1 = directoriesToTest; _a < directoriesToTest_1.length; _a++) {
39+
var directory = directoriesToTest_1[_a];
40+
if (request === "./" + directory + "/index") {
41+
notIndexFile = false;
42+
}
43+
}
44+
var shouldReplaceWithCustomResolve = notIndexFile &&
45+
request.indexOf("src") === -1 &&
3346
resolvedPath.indexOf(path.join(__dirname, "src/" + passingTest.directory)) !== -1;
3447
if (shouldReplaceWithCustomResolve) {
3548
var customResolve = "./" + passingTest.directory;
@@ -43,9 +56,9 @@ var externalsResolver = [
4356
module.exports = {
4457
entry: {
4558
index: "./src/index.ts",
46-
abstractions: "./src/abstractions.ts",
47-
subscribers: "./src/subscribers.ts",
48-
utils: "./src/utils.ts"
59+
abstractions: "./src/abstractions/index.ts",
60+
subscribers: "./src/subscribers/index.ts",
61+
utils: "./src/utils/index.ts"
4962
},
5063
output: {
5164
filename: "./dist/[name].js",

0 commit comments

Comments
 (0)