Comparing version 0.2.0 to 0.3.0
import { Validation, ValidationError, Validator } from "./validation"; | ||
type FieldValue<T> = (T extends number ? number : string) | undefined; | ||
/** | ||
* Utility type to check whether `A` is `never`, and if it's, fallback to `B` instead. | ||
* | ||
* @see https://stackoverflow.com/a/58978075/2083599 | ||
*/ | ||
type Either<A, B> = [A] extends [never] ? B : A; | ||
/** | ||
* The field type as a literal string. | ||
@@ -35,9 +41,9 @@ */ | ||
/** | ||
* Creates a field instance which has a number type. | ||
* Creates a field instance which has a number type. It's optional by default. | ||
*/ | ||
static number(initialValue?: number | null): Field<number>; | ||
static number(initialValue?: number | null): Field<number | undefined>; | ||
/** | ||
* Creates a field instance which has a text type. | ||
* Creates a field instance which has a text type. It's optional by default. | ||
*/ | ||
static text(initialValue?: string | null): Field<string>; | ||
static text(initialValue?: string | null): Field<string | undefined>; | ||
/** | ||
@@ -53,3 +59,3 @@ * Updates the underlying value of the field. | ||
*/ | ||
abstract addValidators<U extends T = T>(...validators: Validator<T, U>[]): Field<U>; | ||
abstract addValidators<U extends T = T>(...validators: Validator<T, U>[]): Field<Either<U, T>>; | ||
/** | ||
@@ -56,0 +62,0 @@ * Builds and returns an observable bag of handy React props for rendering an input or textarea |
@@ -42,3 +42,3 @@ "use strict"; | ||
/** | ||
* Creates a field instance which has a number type. | ||
* Creates a field instance which has a number type. It's optional by default. | ||
*/ | ||
@@ -49,3 +49,3 @@ static number(initialValue) { | ||
/** | ||
* Creates a field instance which has a text type. | ||
* Creates a field instance which has a text type. It's optional by default. | ||
*/ | ||
@@ -52,0 +52,0 @@ static text(initialValue) { |
@@ -29,2 +29,5 @@ import { Field } from "./field"; | ||
export declare class Form<T extends FormDataMap> { | ||
/** | ||
* @see `addValidators` for generic type explanation | ||
*/ | ||
private readonly validators; | ||
@@ -52,5 +55,10 @@ /** | ||
* Add one or more validators to this form. | ||
* | ||
* Note that the validator's "invalid value" is a `FormSnapshot`, which is the type of a | ||
* validated form. That's because the underlying data is validated at the field level before | ||
* additional `Form` validators run. | ||
* | ||
* @returns self, for chaining | ||
*/ | ||
addValidators(...validators: Validator<InvalidFormSnapshot<T>, FormSnapshot<T>>[]): this; | ||
addValidators(...validators: Validator<FormSnapshot<T>, FormSnapshot<T>>[]): this; | ||
private validateAll; | ||
@@ -96,6 +104,11 @@ } | ||
* Add one or more validators to this form array. | ||
* | ||
* Note that the validator's "invalid value" is a `FormSnapshot`, which is the type of a | ||
* validated form. That's because the underlying data is validated at the field level before | ||
* additional `FormArray` validators run. | ||
* | ||
* @returns self, for chaining | ||
*/ | ||
addValidators(...validators: Validator<InvalidFormSnapshot<T[]>, FormSnapshot<T[]>>[]): this; | ||
addValidators(...validators: Validator<FormSnapshot<T[]>, FormSnapshot<T[]>>[]): this; | ||
private validateAll; | ||
} |
@@ -14,2 +14,5 @@ "use strict"; | ||
class Form { | ||
/** | ||
* @see `addValidators` for generic type explanation | ||
*/ | ||
validators = []; | ||
@@ -58,4 +61,11 @@ /** | ||
* Add one or more validators to this form. | ||
* | ||
* Note that the validator's "invalid value" is a `FormSnapshot`, which is the type of a | ||
* validated form. That's because the underlying data is validated at the field level before | ||
* additional `Form` validators run. | ||
* | ||
* @returns self, for chaining | ||
*/ | ||
// TODO: In the future, it might be nice to allow refining the type, so that e.g. an array is | ||
// cast to a tuple via a validator. | ||
addValidators(...validators) { | ||
@@ -69,3 +79,3 @@ this.validators.push(...validators); | ||
if (invalid) { | ||
return validation_1.AGGREGATE_ERROR; | ||
throw validation_1.AGGREGATE_ERROR; | ||
} | ||
@@ -130,4 +140,11 @@ } | ||
* Add one or more validators to this form array. | ||
* | ||
* Note that the validator's "invalid value" is a `FormSnapshot`, which is the type of a | ||
* validated form. That's because the underlying data is validated at the field level before | ||
* additional `FormArray` validators run. | ||
* | ||
* @returns self, for chaining | ||
*/ | ||
// TODO: In the future, it might be nice to allow refining the type, so that e.g. an array is | ||
// cast to a tuple via a validator. | ||
addValidators(...validators) { | ||
@@ -141,3 +158,3 @@ this.validators.push(...validators); | ||
if (invalid) { | ||
return validation_1.AGGREGATE_ERROR; | ||
throw validation_1.AGGREGATE_ERROR; | ||
} | ||
@@ -144,0 +161,0 @@ } |
@@ -38,3 +38,7 @@ type PendingValidation = { | ||
*/ | ||
export type Validator<InvalidValue, Value extends InvalidValue = InvalidValue> = ((value: InvalidValue) => asserts value is Value) | ((value: InvalidValue) => any); | ||
export type Validator<InvalidValue, Value extends InvalidValue = InvalidValue> = ((value: InvalidValue) => asserts value is Value) | ((value: InvalidValue) => Promise<Value> | Value) | { | ||
validate(value: InvalidValue): asserts value is Value; | ||
} | { | ||
validate(value: InvalidValue): Promise<Value> | Value; | ||
}; | ||
export declare class ValidationError extends Error { | ||
@@ -41,0 +45,0 @@ /** |
@@ -59,8 +59,11 @@ "use strict"; | ||
try { | ||
result = await validator(value); | ||
if (result === exports.AGGREGATE_ERROR) { | ||
break; | ||
} | ||
result = await (typeof validator === "function" | ||
? validator(value) | ||
: validator.validate(value)); | ||
} | ||
catch (e) { | ||
if (e === exports.AGGREGATE_ERROR) { | ||
result = e; | ||
return; | ||
} | ||
const error = ValidationError.from(e); | ||
@@ -67,0 +70,0 @@ errors.push(error); |
@@ -5,3 +5,3 @@ { | ||
"repository": "gustavohenke/fielded", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"license": "MIT", | ||
@@ -8,0 +8,0 @@ "homepage": "https://gustavohenke.github.io/fielded/", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
40120
683
0