Comparing version 0.2.0-beta.10 to 0.2.0-beta.11
import { BehaviorSubject, Observable, Subject } from 'rxjs'; | ||
import { IValidator, ValidateStrategy, IMaybeErrors } from './validate'; | ||
import { IValidator, ValidateStrategy, IMaybeError } from './validate'; | ||
export declare enum FormStrategy { | ||
@@ -15,7 +15,7 @@ Model = 0, | ||
attached: boolean; | ||
readonly error$: BehaviorSubject<IMaybeErrors<Value>>; | ||
readonly error$: BehaviorSubject<IMaybeError<Value>>; | ||
abstract isValid(): boolean; | ||
abstract patchValue(value: Value): void; | ||
abstract resetValue(): void; | ||
error: IMaybeErrors<Value>; | ||
error: IMaybeError<Value>; | ||
initialize(value: Value): void; | ||
@@ -22,0 +22,0 @@ validate(strategy?: ValidateStrategy): void; |
@@ -6,2 +6,3 @@ import { RefObject } from 'react'; | ||
export interface IValidateResult<T> { | ||
name: string; | ||
message?: string; | ||
@@ -12,5 +13,3 @@ expect?: T; | ||
} | ||
export declare type IMaybeErrors<T> = { | ||
[key: string]: IValidateResult<T>; | ||
} | null; | ||
export declare type IMaybeError<T> = IValidateResult<T> | null; | ||
export declare enum ValidateStrategy { | ||
@@ -27,13 +26,11 @@ Normal = 0, | ||
(input: Value): ValidatorResult<Value> | null; | ||
name: string; | ||
displayName?: string; | ||
isAsync?: boolean; | ||
} | ||
export declare type ValidatorResult<T> = IValidateResult<T> | null | Promise<IValidateResult<T> | null> | Observable<IValidateResult<T> | null>; | ||
export declare function validate<T>(model: BasicModel<T>, form: FormModel): OperatorFunction<ValidateEvent<T>, IMaybeErrors<T>>; | ||
export declare class ErrorSubscriber<T> implements NextObserver<IMaybeErrors<T>> { | ||
export declare function validate<T>(model: BasicModel<T>, form: FormModel): OperatorFunction<ValidateEvent<T>, IMaybeError<T>>; | ||
export declare class ErrorSubscriber<T> implements NextObserver<IMaybeError<T>> { | ||
private readonly model; | ||
constructor(model: BasicModel<T>); | ||
next(error: IMaybeErrors<T>): void; | ||
next(error: IMaybeError<T>): void; | ||
} | ||
export declare function filterWithCompositing(compositingRef: RefObject<boolean>): () => boolean; |
@@ -13,5 +13,4 @@ "use strict"; | ||
var ValidatorResultSubscriber = /** @class */ (function () { | ||
function ValidatorResultSubscriber(subscriber, validator, original$) { | ||
function ValidatorResultSubscriber(subscriber, original$) { | ||
this.subscriber = subscriber; | ||
this.validator = validator; | ||
this.original$ = original$; | ||
@@ -24,3 +23,3 @@ this.closed = false; | ||
} | ||
this.subscriber.childResult(this.validator, result); | ||
this.subscriber.childResult(result); | ||
}; | ||
@@ -60,15 +59,13 @@ ValidatorResultSubscriber.prototype.error = function (err) { | ||
_this.$validators = new Map(); | ||
_this.errors = null; | ||
return _this; | ||
} | ||
ValidateSubscriber.prototype.childResult = function (validator, error) { | ||
if (error === null || !this.destination) { | ||
ValidateSubscriber.prototype.childResult = function (error) { | ||
if (this.error !== null || error === null || !this.destination) { | ||
return; | ||
} | ||
if (!this.errors) { | ||
this.errors = {}; | ||
} | ||
var name = validator.displayName || validator.name; | ||
this.errors[name] = error; | ||
this._destinationNext(); | ||
/** | ||
* one error returned, stop the others | ||
*/ | ||
this._clear(); | ||
this._destinationNext(error); | ||
}; | ||
@@ -95,2 +92,3 @@ ValidateSubscriber.prototype.childComplete = function (child) { | ||
this._clear(); | ||
this._destinationNext(null); | ||
var _b = this.model, validators = _b.validators, touched = _b.touched; | ||
@@ -115,3 +113,3 @@ if (!touched && !(strategy & ValidateStrategy.IgnoreTouched)) { | ||
var result$ = rxjs_1.from(result); | ||
var subscriber = new ValidatorResultSubscriber(this, validator, result$); | ||
var subscriber = new ValidatorResultSubscriber(this, result$); | ||
var $ = result$.subscribe(subscriber); | ||
@@ -125,3 +123,3 @@ this.$validators.set(subscriber, $); | ||
} | ||
var subscriber = new ValidatorResultSubscriber(this, validator, result); | ||
var subscriber = new ValidatorResultSubscriber(this, result); | ||
var $ = result.subscribe(subscriber); | ||
@@ -132,24 +130,14 @@ this.$validators.set(subscriber, $); | ||
else { | ||
this.childResult(validator, result); | ||
this._clear(); | ||
this._destinationNext(result); | ||
return; | ||
} | ||
} | ||
/** | ||
* this._clear set this.errors to null | ||
* and if this.$validators is empty | ||
* means no error is returned | ||
*/ | ||
if (this.errors === null) { | ||
this._destinationNext(); | ||
} | ||
}; | ||
ValidateSubscriber.prototype._clear = function () { | ||
this.$validators.forEach(function ($) { | ||
$.unsubscribe(); | ||
}); | ||
this.$validators.forEach(function ($) { return $.unsubscribe(); }); | ||
this.$validators.clear(); | ||
this.errors = null; | ||
this._destinationNext(); | ||
}; | ||
ValidateSubscriber.prototype._destinationNext = function () { | ||
this.destination.next && this.destination.next(this.errors); | ||
ValidateSubscriber.prototype._destinationNext = function (error) { | ||
this.destination.next && this.destination.next(error); | ||
}; | ||
@@ -156,0 +144,0 @@ ValidateSubscriber.prototype.unsubscribe = function () { |
@@ -18,2 +18,3 @@ "use strict"; | ||
return { | ||
name: 'min', | ||
actual: value, | ||
@@ -27,2 +28,3 @@ limit: limit, | ||
return { | ||
name: 'min', | ||
actual: value, | ||
@@ -46,2 +48,3 @@ limit: limit, | ||
return { | ||
name: 'max', | ||
actual: value, | ||
@@ -55,2 +58,3 @@ limit: limit, | ||
return { | ||
name: 'max', | ||
actual: value, | ||
@@ -69,2 +73,3 @@ limit: limit, | ||
? { | ||
name: 'required', | ||
message: message, | ||
@@ -83,2 +88,3 @@ actual: input, | ||
: { | ||
name: 'requiredTrue', | ||
message: message, | ||
@@ -97,2 +103,3 @@ expect: true, | ||
: { | ||
name: 'email', | ||
message: message, | ||
@@ -109,2 +116,3 @@ actual: input, | ||
? { | ||
name: 'minLength', | ||
message: message, | ||
@@ -123,2 +131,3 @@ actual: input, | ||
? { | ||
name: 'maxLength', | ||
message: message, | ||
@@ -137,2 +146,3 @@ actual: input, | ||
? { | ||
name: 'pattern', | ||
message: message, | ||
@@ -139,0 +149,0 @@ actual: input, |
import { BehaviorSubject, Observable, Subject } from 'rxjs'; | ||
import { IValidator, ValidateStrategy, IMaybeErrors } from './validate'; | ||
import { IValidator, ValidateStrategy, IMaybeError } from './validate'; | ||
export declare enum FormStrategy { | ||
@@ -15,7 +15,7 @@ Model = 0, | ||
attached: boolean; | ||
readonly error$: BehaviorSubject<IMaybeErrors<Value>>; | ||
readonly error$: BehaviorSubject<IMaybeError<Value>>; | ||
abstract isValid(): boolean; | ||
abstract patchValue(value: Value): void; | ||
abstract resetValue(): void; | ||
error: IMaybeErrors<Value>; | ||
error: IMaybeError<Value>; | ||
initialize(value: Value): void; | ||
@@ -22,0 +22,0 @@ validate(strategy?: ValidateStrategy): void; |
@@ -6,2 +6,3 @@ import { RefObject } from 'react'; | ||
export interface IValidateResult<T> { | ||
name: string; | ||
message?: string; | ||
@@ -12,5 +13,3 @@ expect?: T; | ||
} | ||
export declare type IMaybeErrors<T> = { | ||
[key: string]: IValidateResult<T>; | ||
} | null; | ||
export declare type IMaybeError<T> = IValidateResult<T> | null; | ||
export declare enum ValidateStrategy { | ||
@@ -27,13 +26,11 @@ Normal = 0, | ||
(input: Value): ValidatorResult<Value> | null; | ||
name: string; | ||
displayName?: string; | ||
isAsync?: boolean; | ||
} | ||
export declare type ValidatorResult<T> = IValidateResult<T> | null | Promise<IValidateResult<T> | null> | Observable<IValidateResult<T> | null>; | ||
export declare function validate<T>(model: BasicModel<T>, form: FormModel): OperatorFunction<ValidateEvent<T>, IMaybeErrors<T>>; | ||
export declare class ErrorSubscriber<T> implements NextObserver<IMaybeErrors<T>> { | ||
export declare function validate<T>(model: BasicModel<T>, form: FormModel): OperatorFunction<ValidateEvent<T>, IMaybeError<T>>; | ||
export declare class ErrorSubscriber<T> implements NextObserver<IMaybeError<T>> { | ||
private readonly model; | ||
constructor(model: BasicModel<T>); | ||
next(error: IMaybeErrors<T>): void; | ||
next(error: IMaybeError<T>): void; | ||
} | ||
export declare function filterWithCompositing(compositingRef: RefObject<boolean>): () => boolean; |
@@ -11,5 +11,4 @@ import * as tslib_1 from "tslib"; | ||
var ValidatorResultSubscriber = /** @class */ (function () { | ||
function ValidatorResultSubscriber(subscriber, validator, original$) { | ||
function ValidatorResultSubscriber(subscriber, original$) { | ||
this.subscriber = subscriber; | ||
this.validator = validator; | ||
this.original$ = original$; | ||
@@ -22,3 +21,3 @@ this.closed = false; | ||
} | ||
this.subscriber.childResult(this.validator, result); | ||
this.subscriber.childResult(result); | ||
}; | ||
@@ -58,15 +57,13 @@ ValidatorResultSubscriber.prototype.error = function (err) { | ||
_this.$validators = new Map(); | ||
_this.errors = null; | ||
return _this; | ||
} | ||
ValidateSubscriber.prototype.childResult = function (validator, error) { | ||
if (error === null || !this.destination) { | ||
ValidateSubscriber.prototype.childResult = function (error) { | ||
if (this.error !== null || error === null || !this.destination) { | ||
return; | ||
} | ||
if (!this.errors) { | ||
this.errors = {}; | ||
} | ||
var name = validator.displayName || validator.name; | ||
this.errors[name] = error; | ||
this._destinationNext(); | ||
/** | ||
* one error returned, stop the others | ||
*/ | ||
this._clear(); | ||
this._destinationNext(error); | ||
}; | ||
@@ -93,2 +90,3 @@ ValidateSubscriber.prototype.childComplete = function (child) { | ||
this._clear(); | ||
this._destinationNext(null); | ||
var _b = this.model, validators = _b.validators, touched = _b.touched; | ||
@@ -113,3 +111,3 @@ if (!touched && !(strategy & ValidateStrategy.IgnoreTouched)) { | ||
var result$ = from(result); | ||
var subscriber = new ValidatorResultSubscriber(this, validator, result$); | ||
var subscriber = new ValidatorResultSubscriber(this, result$); | ||
var $ = result$.subscribe(subscriber); | ||
@@ -123,3 +121,3 @@ this.$validators.set(subscriber, $); | ||
} | ||
var subscriber = new ValidatorResultSubscriber(this, validator, result); | ||
var subscriber = new ValidatorResultSubscriber(this, result); | ||
var $ = result.subscribe(subscriber); | ||
@@ -130,24 +128,14 @@ this.$validators.set(subscriber, $); | ||
else { | ||
this.childResult(validator, result); | ||
this._clear(); | ||
this._destinationNext(result); | ||
return; | ||
} | ||
} | ||
/** | ||
* this._clear set this.errors to null | ||
* and if this.$validators is empty | ||
* means no error is returned | ||
*/ | ||
if (this.errors === null) { | ||
this._destinationNext(); | ||
} | ||
}; | ||
ValidateSubscriber.prototype._clear = function () { | ||
this.$validators.forEach(function ($) { | ||
$.unsubscribe(); | ||
}); | ||
this.$validators.forEach(function ($) { return $.unsubscribe(); }); | ||
this.$validators.clear(); | ||
this.errors = null; | ||
this._destinationNext(); | ||
}; | ||
ValidateSubscriber.prototype._destinationNext = function () { | ||
this.destination.next && this.destination.next(this.errors); | ||
ValidateSubscriber.prototype._destinationNext = function (error) { | ||
this.destination.next && this.destination.next(error); | ||
}; | ||
@@ -154,0 +142,0 @@ ValidateSubscriber.prototype.unsubscribe = function () { |
@@ -16,2 +16,3 @@ import Decimal from 'big.js'; | ||
return { | ||
name: 'min', | ||
actual: value, | ||
@@ -25,2 +26,3 @@ limit: limit, | ||
return { | ||
name: 'min', | ||
actual: value, | ||
@@ -43,2 +45,3 @@ limit: limit, | ||
return { | ||
name: 'max', | ||
actual: value, | ||
@@ -52,2 +55,3 @@ limit: limit, | ||
return { | ||
name: 'max', | ||
actual: value, | ||
@@ -65,2 +69,3 @@ limit: limit, | ||
? { | ||
name: 'required', | ||
message: message, | ||
@@ -78,2 +83,3 @@ actual: input, | ||
: { | ||
name: 'requiredTrue', | ||
message: message, | ||
@@ -91,2 +97,3 @@ expect: true, | ||
: { | ||
name: 'email', | ||
message: message, | ||
@@ -102,2 +109,3 @@ actual: input, | ||
? { | ||
name: 'minLength', | ||
message: message, | ||
@@ -115,2 +123,3 @@ actual: input, | ||
? { | ||
name: 'maxLength', | ||
message: message, | ||
@@ -128,2 +137,3 @@ actual: input, | ||
? { | ||
name: 'pattern', | ||
message: message, | ||
@@ -130,0 +140,0 @@ actual: input, |
{ | ||
"name": "formulr", | ||
"version": "0.2.0-beta.10", | ||
"version": "0.2.0-beta.11", | ||
"description": "Form toolkit for React", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
import { BehaviorSubject, Observable, Subject } from 'rxjs'; | ||
import { IValidator, ValidateStrategy, IMaybeErrors } from './validate'; | ||
import { IValidator, ValidateStrategy, IMaybeError } from './validate'; | ||
@@ -19,3 +19,3 @@ export enum FormStrategy { | ||
readonly error$ = new BehaviorSubject<IMaybeErrors<Value>>(null); | ||
readonly error$ = new BehaviorSubject<IMaybeError<Value>>(null); | ||
@@ -30,3 +30,3 @@ abstract isValid(): boolean; | ||
set error(error: IMaybeErrors<Value>) { | ||
set error(error: IMaybeError<Value>) { | ||
this.error$.next(error); | ||
@@ -33,0 +33,0 @@ } |
@@ -19,2 +19,3 @@ import { RefObject } from 'react'; | ||
export interface IValidateResult<T> { | ||
name: string; | ||
message?: string; | ||
@@ -26,5 +27,3 @@ expect?: T; | ||
export type IMaybeErrors<T> = { | ||
[key: string]: IValidateResult<T>; | ||
} | null; | ||
export type IMaybeError<T> = IValidateResult<T> | null; | ||
@@ -44,4 +43,2 @@ export enum ValidateStrategy { | ||
(input: Value): ValidatorResult<Value> | null; | ||
name: string; | ||
displayName?: string; | ||
isAsync?: boolean; | ||
@@ -61,3 +58,2 @@ } | ||
private readonly subscriber: ValidateSubscriber<T>, | ||
private readonly validator: IValidator<T>, | ||
readonly original$: Observable<IValidateResult<T> | null>, | ||
@@ -70,3 +66,3 @@ ) {} | ||
} | ||
this.subscriber.childResult(this.validator, result); | ||
this.subscriber.childResult(result); | ||
} | ||
@@ -91,6 +87,6 @@ | ||
class ValidateOperator<T> implements Operator<ValidateEvent<T>, IMaybeErrors<T>> { | ||
class ValidateOperator<T> implements Operator<ValidateEvent<T>, IMaybeError<T>> { | ||
constructor(private readonly model: BasicModel<T>, private readonly form: FormModel) {} | ||
call(subscriber: Subscriber<IMaybeErrors<T>>, source: Observable<ValidateEvent<T>>) { | ||
call(subscriber: Subscriber<IMaybeError<T>>, source: Observable<ValidateEvent<T>>) { | ||
return source.subscribe(new ValidateSubscriber(subscriber, this.model, this.form)); | ||
@@ -103,6 +99,4 @@ } | ||
private errors: IMaybeErrors<T> = null; | ||
constructor( | ||
destination: Subscriber<IMaybeErrors<T>>, | ||
destination: Subscriber<IMaybeError<T>>, | ||
private readonly model: BasicModel<T>, | ||
@@ -114,12 +108,11 @@ private readonly form: FormModel, | ||
childResult(validator: IValidator<T>, error: IValidateResult<T> | null) { | ||
if (error === null || !this.destination) { | ||
childResult(error: IValidateResult<T> | null) { | ||
if (this.error !== null || error === null || !this.destination) { | ||
return; | ||
} | ||
if (!this.errors) { | ||
this.errors = {}; | ||
} | ||
const name = validator.displayName || validator.name; | ||
this.errors[name] = error; | ||
this._destinationNext(); | ||
/** | ||
* one error returned, stop the others | ||
*/ | ||
this._clear(); | ||
this._destinationNext(error); | ||
} | ||
@@ -149,2 +142,3 @@ | ||
this._clear(); | ||
this._destinationNext(null); | ||
const { validators, touched } = this.model; | ||
@@ -169,3 +163,3 @@ if (!touched && !(strategy & ValidateStrategy.IgnoreTouched)) { | ||
const result$ = from(result); | ||
const subscriber = new ValidatorResultSubscriber(this, validator, result$); | ||
const subscriber = new ValidatorResultSubscriber(this, result$); | ||
const $ = result$.subscribe(subscriber); | ||
@@ -178,3 +172,3 @@ this.$validators.set(subscriber, $); | ||
} | ||
const subscriber = new ValidatorResultSubscriber(this, validator, result); | ||
const subscriber = new ValidatorResultSubscriber(this, result); | ||
const $ = result.subscribe(subscriber); | ||
@@ -184,26 +178,16 @@ this.$validators.set(subscriber, $); | ||
} else { | ||
this.childResult(validator, result); | ||
this._clear(); | ||
this._destinationNext(result); | ||
return; | ||
} | ||
} | ||
/** | ||
* this._clear set this.errors to null | ||
* and if this.$validators is empty | ||
* means no error is returned | ||
*/ | ||
if (this.errors === null) { | ||
this._destinationNext(); | ||
} | ||
} | ||
private _clear() { | ||
this.$validators.forEach($ => { | ||
$.unsubscribe(); | ||
}); | ||
this.$validators.forEach($ => $.unsubscribe()); | ||
this.$validators.clear(); | ||
this.errors = null; | ||
this._destinationNext(); | ||
} | ||
private _destinationNext() { | ||
this.destination.next && this.destination.next(this.errors); | ||
private _destinationNext(error: IMaybeError<T>) { | ||
this.destination.next && this.destination.next(error); | ||
} | ||
@@ -217,7 +201,4 @@ | ||
export function validate<T>( | ||
model: BasicModel<T>, | ||
form: FormModel, | ||
): OperatorFunction<ValidateEvent<T>, IMaybeErrors<T>> { | ||
return function validateOperation(source: Observable<ValidateEvent<T>>): Observable<IMaybeErrors<T>> { | ||
export function validate<T>(model: BasicModel<T>, form: FormModel): OperatorFunction<ValidateEvent<T>, IMaybeError<T>> { | ||
return function validateOperation(source: Observable<ValidateEvent<T>>): Observable<IMaybeError<T>> { | ||
return source.lift(new ValidateOperator(model, form)); | ||
@@ -227,6 +208,6 @@ }; | ||
export class ErrorSubscriber<T> implements NextObserver<IMaybeErrors<T>> { | ||
export class ErrorSubscriber<T> implements NextObserver<IMaybeError<T>> { | ||
constructor(private readonly model: BasicModel<T>) {} | ||
next(error: IMaybeErrors<T>) { | ||
next(error: IMaybeError<T>) { | ||
this.model.error = error; | ||
@@ -233,0 +214,0 @@ } |
@@ -20,2 +20,3 @@ import Decimal from 'big.js'; | ||
return { | ||
name: 'min', | ||
actual: value, | ||
@@ -28,2 +29,3 @@ limit, | ||
return { | ||
name: 'min', | ||
actual: value, | ||
@@ -47,2 +49,3 @@ limit, | ||
return { | ||
name: 'max', | ||
actual: value, | ||
@@ -55,2 +58,3 @@ limit, | ||
return { | ||
name: 'max', | ||
actual: value, | ||
@@ -69,2 +73,3 @@ limit, | ||
? { | ||
name: 'required', | ||
message, | ||
@@ -83,2 +88,3 @@ actual: input, | ||
: { | ||
name: 'requiredTrue', | ||
message, | ||
@@ -97,2 +103,3 @@ expect: true, | ||
: { | ||
name: 'email', | ||
message, | ||
@@ -113,2 +120,3 @@ actual: input, | ||
? { | ||
name: 'minLength', | ||
message, | ||
@@ -127,2 +135,3 @@ actual: input, | ||
? { | ||
name: 'maxLength', | ||
message, | ||
@@ -141,2 +150,3 @@ actual: input, | ||
? { | ||
name: 'pattern', | ||
message, | ||
@@ -143,0 +153,0 @@ actual: input, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
102546
2944