Skip to content

Commit a14b14f

Browse files
Validation updated to suite latest simplr-forms-core changes.
1 parent f942e23 commit a14b14f

File tree

17 files changed

+94
-76
lines changed

17 files changed

+94
-76
lines changed

packages/simplr-validation/__tests__/subscribers/form-store-subscriber.test.tsx

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
import * as React from "react";
2-
import { Stores, Actions, Contracts as FormsCoreContracts } from "simplr-forms-core";
32
import * as Sinon from "sinon";
43

4+
import {
5+
FieldValue,
6+
FieldValidationType,
7+
FieldStateProps
8+
} from "simplr-forms-core/contracts";
9+
import { FormStore } from "simplr-forms-core/stores";
10+
import {
11+
FieldRegistered,
12+
PropsChanged,
13+
ValueChanged,
14+
} from "simplr-forms-core/actions";
15+
516
import { ContainsValidator } from "../../src/validators/index";
617
import { FormStoreSubscriber } from "../../src/subscribers/form-store-subscriber";
718

8-
const { FieldValidationType } = FormsCoreContracts;
9-
1019
class MySubscriber extends FormStoreSubscriber {
1120
public ValidateField(
1221
fieldId: string,
13-
value: FormsCoreContracts.FieldValue,
14-
validationType: FormsCoreContracts.FieldValidationType
22+
value: FieldValue,
23+
validationType: FieldValidationType
1524
) {
1625
return super.ValidateField(fieldId, value, validationType);
1726
}
@@ -28,25 +37,25 @@ describe("FormStoreSubscriber", () => {
2837
});
2938

3039
it("add listeners to form store", () => {
31-
const formStore = new Stores.FormStore("form-id");
32-
const callback = sandbox.spy(Stores.FormStore.prototype, "addListener");
40+
const formStore = new FormStore("form-id");
41+
const callback = sandbox.spy(FormStore.prototype, "addListener");
3342

3443
expect(callback.called).toEqual(false);
3544

36-
expect(formStore.listeners(Actions.FieldRegistered).length).toBe(0);
37-
expect(formStore.listeners(Actions.PropsChanged).length).toBe(0);
38-
expect(formStore.listeners(Actions.ValueChanged).length).toBe(0);
45+
expect(formStore.listeners(FieldRegistered).length).toBe(0);
46+
expect(formStore.listeners(PropsChanged).length).toBe(0);
47+
expect(formStore.listeners(ValueChanged).length).toBe(0);
3948

4049
new FormStoreSubscriber(formStore);
4150
expect(callback.called).toEqual(true);
4251

43-
expect(formStore.listeners(Actions.FieldRegistered).length).toBe(1);
44-
expect(formStore.listeners(Actions.PropsChanged).length).toBe(1);
45-
expect(formStore.listeners(Actions.ValueChanged).length).toBe(1);
52+
expect(formStore.listeners(FieldRegistered).length).toBe(1);
53+
expect(formStore.listeners(PropsChanged).length).toBe(1);
54+
expect(formStore.listeners(ValueChanged).length).toBe(1);
4655
});
4756

4857
it("remove listeners from form store", () => {
49-
const formStore = new Stores.FormStore("form-id");
58+
const formStore = new FormStore("form-id");
5059

5160
const formSubscriber = new MySubscriber(formStore);
5261

@@ -65,11 +74,11 @@ describe("FormStoreSubscriber", () => {
6574

6675
const validatorValidateCallback = sandbox.spy(ContainsValidator.prototype, "Validate");
6776
const fieldChildren = [<ContainsValidator value="valid" error={errorMessage} />];
68-
const formStore = new Stores.FormStore("form-id");
69-
const formStoreValidateCallback = sandbox.spy(Stores.FormStore.prototype, "Validate");
77+
const formStore = new FormStore("form-id");
78+
const formStoreValidateCallback = sandbox.spy(FormStore.prototype, "Validate");
7079
const subscriber = new MySubscriber(formStore);
7180

72-
const fieldProps: FormsCoreContracts.FieldStateProps = {
81+
const fieldProps: FieldStateProps = {
7382
name: "field-name",
7483
children: fieldChildren,
7584
validationType: FieldValidationType.OnValueChange
@@ -95,11 +104,11 @@ describe("FormStoreSubscriber", () => {
95104

96105
const validatorValidateCallback = sandbox.spy(ContainsValidator.prototype, "Validate");
97106
const fieldChildren = [<ContainsValidator value="valid" error={errorMessage} />];
98-
const formStore = new Stores.FormStore("form-id");
99-
const formStoreValidateCallback = sandbox.spy(Stores.FormStore.prototype, "Validate");
107+
const formStore = new FormStore("form-id");
108+
const formStoreValidateCallback = sandbox.spy(FormStore.prototype, "Validate");
100109
const subscriber = new MySubscriber(formStore);
101110

102-
const fieldProps: FormsCoreContracts.FieldStateProps = {
111+
const fieldProps: FieldStateProps = {
103112
name: "field-name",
104113
children: fieldChildren,
105114
validationType: FieldValidationType.OnValueChange
@@ -125,11 +134,11 @@ describe("FormStoreSubscriber", () => {
125134

126135
const validatorValidateCallback = sandbox.spy(ContainsValidator.prototype, "Validate");
127136
const fieldChildren = [<ContainsValidator value="valid" error={errorMessage} />];
128-
const formStore = new Stores.FormStore("form-id");
129-
const formStoreValidateCallback = sandbox.spy(Stores.FormStore.prototype, "Validate");
137+
const formStore = new FormStore("form-id");
138+
const formStoreValidateCallback = sandbox.spy(FormStore.prototype, "Validate");
130139
const subscriber = new MySubscriber(formStore);
131140

132-
const fieldProps: FormsCoreContracts.FieldStateProps = {
141+
const fieldProps: FieldStateProps = {
133142
name: "field-name",
134143
children: fieldChildren,
135144
validationType: FieldValidationType.OnPropsChange
@@ -149,8 +158,8 @@ describe("FormStoreSubscriber", () => {
149158
const initialValue = "initial value";
150159

151160
const validatorValidateCallback = sandbox.spy(ContainsValidator.prototype, "Validate");
152-
const formStore = new Stores.FormStore("form-id");
153-
const formStoreValidateCallback = sandbox.spy(Stores.FormStore.prototype, "Validate");
161+
const formStore = new FormStore("form-id");
162+
const formStoreValidateCallback = sandbox.spy(FormStore.prototype, "Validate");
154163
const subscriber = new MySubscriber(formStore);
155164

156165
// Validation is skipped because props are undefined

packages/simplr-validation/src/abstractions/base-validator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from "react";
2-
import { Contracts as FormsCoreContracts } from "simplr-forms-core";
2+
import { FieldValue } from "simplr-forms-core/contracts";
33

44
import { Validator, ValidationResult, ValidationError } from "../contracts";
55

@@ -11,7 +11,7 @@ export abstract class BaseValidator<TProps extends ValidatorProps>
1111
extends React.Component<TProps, {}> implements Validator {
1212
static SimplrValidationValidatorComponent(): void { }
1313

14-
abstract Validate(value: FormsCoreContracts.FieldValue): ValidationResult;
14+
abstract Validate(value: FieldValue): ValidationResult;
1515

1616
protected SkipValidation(value: any) {
1717
return (value == null || value === "");

packages/simplr-validation/src/contracts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { Contracts as FormsCoreContracts } from "simplr-forms-core";
1+
import { FormError } from "simplr-forms-core/contracts";
22

33
export interface Validator {
44
Validate(value: any): ValidationResult;
55
}
66

7-
export type ValidationError = string | FormsCoreContracts.FormError;
7+
export type ValidationError = string | FormError;
88

99
export type ValidationResult = Promise<void> | ValidationError | undefined;
1010

packages/simplr-validation/src/subscribers/form-store-subscriber.ts

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
import * as React from "react";
2-
import { Stores, Actions, Contracts as FormsCoreContracts } from "simplr-forms-core";
32
import * as ActionEmitter from "action-emitter";
43

5-
import { Validate } from "../utils/validation";
4+
import {
5+
FieldValue,
6+
FieldValidationType
7+
} from "simplr-forms-core/contracts";
8+
import { FormStore } from "simplr-forms-core/stores";
9+
import {
10+
FieldRegistered,
11+
ValueChanged,
12+
PropsChanged
13+
} from "simplr-forms-core/actions";
614

7-
const { FieldValidationType } = FormsCoreContracts;
15+
import { Validate } from "../utils/validation";
816

917
export class FormStoreSubscriber {
1018

1119
private fieldOnRegisteredSubscription?: ActionEmitter.EventSubscription;
1220
private fieldOnValueChangedSubscription?: ActionEmitter.EventSubscription;
1321
private fieldOnPropsChangedSubscription?: ActionEmitter.EventSubscription;
1422

15-
constructor(private formStore: Stores.FormStore) {
16-
this.fieldOnRegisteredSubscription = this.formStore.addListener(Actions.FieldRegistered, this.OnRegistered.bind(this));
17-
this.fieldOnValueChangedSubscription = this.formStore.addListener(Actions.ValueChanged, this.OnValueChanged.bind(this));
18-
this.fieldOnPropsChangedSubscription = this.formStore.addListener(Actions.PropsChanged, this.OnPropsChanged.bind(this));
23+
constructor(private formStore: FormStore) {
24+
this.fieldOnRegisteredSubscription = this.formStore.addListener(FieldRegistered, this.OnRegistered.bind(this));
25+
this.fieldOnValueChangedSubscription = this.formStore.addListener(ValueChanged, this.OnValueChanged.bind(this));
26+
this.fieldOnPropsChangedSubscription = this.formStore.addListener(PropsChanged, this.OnPropsChanged.bind(this));
1927
}
2028

2129
public RemoveFormListeners() {
@@ -33,8 +41,8 @@ export class FormStoreSubscriber {
3341

3442
protected async ValidateField(
3543
fieldId: string,
36-
value: FormsCoreContracts.FieldValue,
37-
validationType: FormsCoreContracts.FieldValidationType
44+
value: FieldValue,
45+
validationType: FieldValidationType
3846
) {
3947
const fieldState = this.formStore.GetField(fieldId);
4048
const fieldProps = fieldState.Props;
@@ -52,15 +60,15 @@ export class FormStoreSubscriber {
5260
await this.formStore.Validate(fieldId, validationPromise);
5361
}
5462

55-
protected OnRegistered(action: Actions.FieldRegistered) {
63+
protected OnRegistered(action: FieldRegistered) {
5664
this.ValidateField(action.FieldId, action.InitialValue, FieldValidationType.OnFieldRegistered);
5765
}
5866

59-
protected OnValueChanged(action: Actions.ValueChanged) {
67+
protected OnValueChanged(action: ValueChanged) {
6068
this.ValidateField(action.FieldId, action.NewValue, FieldValidationType.OnValueChange);
6169
}
6270

63-
protected OnPropsChanged(action: Actions.PropsChanged) {
71+
protected OnPropsChanged(action: PropsChanged) {
6472
const fieldState = this.formStore.GetField(action.FieldId);
6573
this.ValidateField(action.FieldId, fieldState.Value, FieldValidationType.OnValueChange);
6674
}

packages/simplr-validation/src/subscribers/form-stores-handler-subscriber.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Stores, Actions } from "simplr-forms-core";
1+
import { FSHContainerClass, FSHContainer } from "simplr-forms-core/stores";
2+
import { FormRegistered, FormUnregistered } from "simplr-forms-core/actions";
23
import * as ActionEmitter from "action-emitter";
34
import * as Immutable from "immutable";
45
import { FormStoreSubscriber } from "./form-store-subscriber";
@@ -17,21 +18,21 @@ export class FormStoresHandlerSubscriber {
1718
return this.fshContainer.FormStoresHandler;
1819
}
1920

20-
constructor(private fshContainer: Stores.FSHContainerClass = Stores.FSHContainer) {
21+
constructor(private fshContainer: FSHContainerClass = FSHContainer) {
2122
this.formRegisterSubscription = this.formStoresHandler
22-
.addListener(Actions.FormRegistered, this.onFormRegistered.bind(this));
23+
.addListener(FormRegistered, this.onFormRegistered.bind(this));
2324
this.formUnregisterSubscription = this.formStoresHandler
24-
.addListener(Actions.FormUnregistered, this.onFormUnregistered.bind(this));
25+
.addListener(FormUnregistered, this.onFormUnregistered.bind(this));
2526
}
2627

27-
private onFormRegistered(action: Actions.FormRegistered) {
28+
private onFormRegistered(action: FormRegistered) {
2829
const formStore = this.fshContainer.FormStoresHandler.GetStore(action.FormId);
2930

3031
this.formStoresSubscribers = this.formStoresSubscribers
3132
.set(action.FormId, new FormStoreSubscriber(formStore));
3233
}
3334

34-
private onFormUnregistered(action: Actions.FormUnregistered) {
35+
private onFormUnregistered(action: FormUnregistered) {
3536
const formStoreSubscriber = this.formStoresSubscribers.get(action.FormId);
3637
formStoreSubscriber.RemoveFormListeners();
3738

packages/simplr-validation/src/utils/validation.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import * as ReactDOM from "react-dom";
2-
import { Utils, Contracts } from "simplr-forms-core";
1+
import { FieldValue } from "simplr-forms-core/contracts";
2+
import { ProcessValue } from "simplr-forms-core/utils";
33

44
import { Validator, VALIDATOR_FUNCTION_NAME } from "../contracts";
55

66

7-
export function Validate(components: Array<JSX.Element>, value: Contracts.FieldValue) {
8-
return Utils.ProcessValue<Validator, Promise<void>>(components, value, VALIDATOR_FUNCTION_NAME,
7+
export function Validate(components: Array<JSX.Element>, value: FieldValue) {
8+
return ProcessValue<Validator, Promise<void>>(components, value, VALIDATOR_FUNCTION_NAME,
99
async (processor, value) => {
1010
const validationResult = processor.Validate(value);
1111
if (validationResult != null && typeof validationResult === "string") {

packages/simplr-validation/src/validators/alpha.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Validator from "validator";
2-
import { Contracts } from "simplr-forms-core";
2+
import { FieldValue } from "simplr-forms-core/contracts";
33

44
import { BaseValidator, ValidatorProps } from "../abstractions/base-validator";
55
import { ValidationResult } from "../contracts";
@@ -9,7 +9,7 @@ export interface AlphaValidatorProps extends ValidatorProps {
99
}
1010

1111
export class AlphaValidator extends BaseValidator<AlphaValidatorProps> {
12-
Validate(value: Contracts.FieldValue): ValidationResult {
12+
Validate(value: FieldValue): ValidationResult {
1313
if (this.SkipValidation(value)) {
1414
return this.ValidSync();
1515
}

packages/simplr-validation/src/validators/alphanumeric.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Validator from "validator";
2-
import { Contracts } from "simplr-forms-core";
2+
import { FieldValue } from "simplr-forms-core/contracts";
33

44
import { BaseValidator, ValidatorProps } from "../abstractions/base-validator";
55
import { ValidationResult } from "../contracts";
@@ -9,7 +9,7 @@ export interface AlphanumericValidatorProps extends ValidatorProps {
99
}
1010

1111
export class AlphanumericValidator extends BaseValidator<AlphanumericValidatorProps> {
12-
Validate(value: Contracts.FieldValue): ValidationResult {
12+
Validate(value: FieldValue): ValidationResult {
1313
if (this.SkipValidation(value)) {
1414
return this.ValidSync();
1515
}

packages/simplr-validation/src/validators/ascii.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as Validator from "validator";
2-
import { Contracts } from "simplr-forms-core";
2+
import { FieldValue } from "simplr-forms-core/contracts";
33

44
import { BaseValidator, ValidatorProps } from "../abstractions/base-validator";
55
import { ValidationResult } from "../contracts";
66

77
export interface AsciiValidatorProps extends ValidatorProps { }
88

99
export class AsciiValidator extends BaseValidator<AsciiValidatorProps> {
10-
Validate(value: Contracts.FieldValue): ValidationResult {
10+
Validate(value: FieldValue): ValidationResult {
1111
if (this.SkipValidation(value)) {
1212
return this.ValidSync();
1313
}

packages/simplr-validation/src/validators/base64.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import * as Validator from "validator";
2-
import { Contracts } from "simplr-forms-core";
2+
import { FieldValue } from "simplr-forms-core/contracts";
33

44
import { BaseValidator, ValidatorProps } from "../abstractions/base-validator";
55
import { ValidationResult } from "../contracts";
66

77
export interface Base64ValidatorProps extends ValidatorProps { }
88

99
export class Base64Validator extends BaseValidator<Base64ValidatorProps> {
10-
Validate(value: Contracts.FieldValue): ValidationResult {
10+
Validate(value: FieldValue): ValidationResult {
1111
if (this.SkipValidation(value)) {
1212
return this.ValidSync();
1313
}

0 commit comments

Comments
 (0)