@finnair/v-validation
Advanced tools
Comparing version 7.0.0-alpha.9 to 7.0.0
@@ -6,2 +6,14 @@ # Change Log | ||
# [7.0.0](https://github.com/finnair/v-validation/compare/v7.0.0-alpha.9...v7.0.0) (2025-01-07) | ||
### Features | ||
- V.jsonBigInt() validator for JSON-safe JsonBigInt wrapper for BigInt ([#125](https://github.com/finnair/v-validation/issues/125)) ([b6ae653](https://github.com/finnair/v-validation/commit/b6ae65374d41436577d4e4b6c5ee148f8ad8635c)) | ||
# [7.0.0](https://github.com/finnair/v-validation/compare/v7.0.0-alpha.9...v7.0.0) (2025-01-07) | ||
### Features | ||
- V.jsonBigInt() validator for JSON-safe JsonBigInt wrapper for BigInt ([#125](https://github.com/finnair/v-validation/issues/125)) ([b6ae653](https://github.com/finnair/v-validation/commit/b6ae65374d41436577d4e4b6c5ee148f8ad8635c)) | ||
# [7.0.0-alpha.9](https://github.com/finnair/v-validation/compare/v7.0.0-alpha.8...v7.0.0-alpha.9) (2024-12-11) | ||
@@ -8,0 +20,0 @@ |
import { SchemaValidator, SchemaModel } from './schema.js'; | ||
import { Path } from '@finnair/path'; | ||
import { IgnoreValidator, ArrayNormalizer, AnyValidator, StringValidator, StringNormalizer, NotNullOrUndefinedValidator, IsNullOrUndefinedValidator, NotEmptyValidator, NotBlankValidator, ValueMapper, BooleanValidator, NumberValidator, NumberNormalizer, DateValidator, ValidatorFn, ValidatorFnWrapper, MappingFn, Validator, CheckValidator, OptionalValidator, AssertTrue, IfValidator, GroupOrName, WhenGroupValidator, AssertTrueValidator, PatternValidator, PatternNormalizer, BooleanNormalizer, MinValidator, MaxValidator, MapValidator, MapNormalizer, ArrayValidator, SizeValidator, AnyOfValidator, OneOfValidator, EnumValidator, HasValueValidator, JsonValidator, RequiredValidator, SetValidator, UuidValidator, VType, CompositionParameters, OptionalUndefinedValidator, NullableValidator } from './validators.js'; | ||
import { IgnoreValidator, ArrayNormalizer, AnyValidator, StringValidator, StringNormalizer, NotNullOrUndefinedValidator, IsNullOrUndefinedValidator, NotEmptyValidator, NotBlankValidator, ValueMapper, BooleanValidator, NumberValidator, NumberNormalizer, DateValidator, ValidatorFn, ValidatorFnWrapper, MappingFn, Validator, CheckValidator, OptionalValidator, AssertTrue, IfValidator, GroupOrName, WhenGroupValidator, AssertTrueValidator, PatternValidator, PatternNormalizer, BooleanNormalizer, MinValidator, MaxValidator, MapValidator, MapNormalizer, ArrayValidator, SizeValidator, AnyOfValidator, OneOfValidator, EnumValidator, HasValueValidator, JsonValidator, RequiredValidator, SetValidator, UuidValidator, VType, CompositionParameters, OptionalUndefinedValidator, NullableValidator, JsonBigIntValidator } from './validators.js'; | ||
import { ObjectModel, ObjectValidator, ObjectNormalizer } from './objectValidator.js'; | ||
@@ -58,2 +58,3 @@ import { ObjectValidatorBuilder } from './objectValidatorBuilder.js'; | ||
max: (max: number, inclusive?: boolean) => MaxValidator; | ||
jsonBigInt: () => JsonBigIntValidator; | ||
object: <T, I = T>(model: ObjectModel<T, I>) => ObjectValidator<T, I>; | ||
@@ -60,0 +61,0 @@ objectType: () => ObjectValidatorBuilder<unknown, unknown, unknown, unknown>; |
@@ -9,3 +9,3 @@ "use strict"; | ||
const AllOfConstructor = (...validators) => new validators_js_1.AllOfValidator(validators); | ||
const ignoreValidator = new validators_js_1.IgnoreValidator(), stringValidator = new validators_js_1.StringValidator(), toStringValidator = new validators_js_1.StringNormalizer(), nullOrUndefinedValidator = new validators_js_1.IsNullOrUndefinedValidator(), notBlankValidator = new validators_js_1.NotBlankValidator(), emptyToNullValidator = new validators_js_1.ValueMapper((value) => ((0, validators_js_1.isNullOrUndefined)(value) || value === '' ? null : value)), emptyToUndefinedValidator = new validators_js_1.ValueMapper((value) => ((0, validators_js_1.isNullOrUndefined)(value) || value === '' ? undefined : value)), undefinedToNullValidator = new validators_js_1.ValueMapper((value) => (value === undefined ? null : value)), booleanValidator = new validators_js_1.BooleanValidator(), numberValidator = new validators_js_1.NumberValidator(validators_js_1.NumberFormat.number), toNumberValidator = new validators_js_1.NumberNormalizer(validators_js_1.NumberFormat.number), integerValidator = new validators_js_1.NumberValidator(validators_js_1.NumberFormat.integer), toIntegerValidator = new validators_js_1.NumberNormalizer(validators_js_1.NumberFormat.integer), dateValidator = new validators_js_1.DateValidator(validators_js_1.ValidatorType.Date); | ||
const ignoreValidator = new validators_js_1.IgnoreValidator(), stringValidator = new validators_js_1.StringValidator(), toStringValidator = new validators_js_1.StringNormalizer(), nullOrUndefinedValidator = new validators_js_1.IsNullOrUndefinedValidator(), notBlankValidator = new validators_js_1.NotBlankValidator(), emptyToNullValidator = new validators_js_1.ValueMapper((value) => ((0, validators_js_1.isNullOrUndefined)(value) || value === '' ? null : value)), emptyToUndefinedValidator = new validators_js_1.ValueMapper((value) => ((0, validators_js_1.isNullOrUndefined)(value) || value === '' ? undefined : value)), undefinedToNullValidator = new validators_js_1.ValueMapper((value) => (value === undefined ? null : value)), booleanValidator = new validators_js_1.BooleanValidator(), numberValidator = new validators_js_1.NumberValidator(validators_js_1.NumberFormat.number), toNumberValidator = new validators_js_1.NumberNormalizer(validators_js_1.NumberFormat.number), integerValidator = new validators_js_1.NumberValidator(validators_js_1.NumberFormat.integer), jsonBigIntValidator = new validators_js_1.JsonBigIntValidator(), toIntegerValidator = new validators_js_1.NumberNormalizer(validators_js_1.NumberFormat.integer), dateValidator = new validators_js_1.DateValidator(validators_js_1.ValidatorType.Date); | ||
exports.V = { | ||
@@ -53,2 +53,3 @@ fn: (fn, type) => new validators_js_1.ValidatorFnWrapper(fn, type), | ||
max: (max, inclusive = true) => new validators_js_1.MaxValidator(max, inclusive), | ||
jsonBigInt: () => jsonBigIntValidator, | ||
object: (model) => new objectValidator_js_1.ObjectValidator(model), | ||
@@ -55,0 +56,0 @@ objectType: () => new objectValidatorBuilder_js_1.ObjectValidatorBuilder(), |
@@ -264,2 +264,8 @@ import { Path } from '@finnair/path'; | ||
} | ||
export declare class JsonBigInt { | ||
readonly value: bigint; | ||
constructor(value: bigint); | ||
valueOf(): bigint; | ||
toJSON(): string; | ||
} | ||
export declare class AnyValidator<InOut = any> extends Validator<InOut> { | ||
@@ -331,2 +337,6 @@ validatePath(value: InOut, path: Path, ctx: ValidationContext): PromiseLike<InOut>; | ||
} | ||
export declare class JsonBigIntValidator extends Validator<JsonBigInt, any> { | ||
constructor(); | ||
validatePath(value: any, path: Path, ctx: ValidationContext): PromiseLike<JsonBigInt>; | ||
} | ||
export declare class NextNumberValidator<In> extends NumberValidatorBase<In> { | ||
@@ -333,0 +343,0 @@ readonly firstValidator: Validator<number, any>; |
@@ -6,4 +6,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.NumberValidator = exports.NextNumberValidator = exports.NumberValidatorBase = exports.NumberFormat = exports.BooleanNormalizer = exports.BooleanValidator = exports.NotBlankValidator = exports.SizeValidator = exports.NotEmptyValidator = exports.IsNullOrUndefinedValidator = exports.NotNullOrUndefinedValidator = exports.StringNormalizer = exports.StringValidator = exports.NextStringValidator = exports.StringValidatorBase = exports.AnyValidator = exports.JsonSet = exports.SetValidator = exports.JsonMap = exports.MapNormalizer = exports.MapValidator = exports.WhenGroup = exports.WhenGroupValidator = exports.Conditional = exports.IfValidator = exports.AnyOfValidator = exports.OneOfValidator = exports.CompositionValidator = exports.CheckValidator = exports.ArrayNormalizer = exports.ArrayValidator = exports.ValidatorFnWrapper = exports.defaultViolations = exports.ValidatorType = exports.Groups = exports.Group = exports.SizeViolation = exports.MaxViolation = exports.MinViolation = exports.OneOfMismatch = exports.PatternViolation = exports.HasValueViolation = exports.ErrorViolation = exports.EnumMismatch = exports.TypeMismatch = exports.Violation = exports.ValidationError = exports.ValidationResult = exports.Validator = exports.ValidationContext = void 0; | ||
exports.JsonValidator = exports.IgnoreValidator = exports.ValueMapper = exports.RequiredValidator = exports.NullableValidator = exports.OptionalUndefinedValidator = exports.OptionalValidator = exports.PatternNormalizer = exports.PatternValidator = exports.DateValidator = exports.AllOfValidator = exports.HasValueValidator = exports.UuidValidator = exports.AssertTrueValidator = exports.EnumValidator = exports.MaxValidator = exports.MinValidator = exports.NumberNormalizer = void 0; | ||
exports.JsonBigIntValidator = exports.NumberValidatorBase = exports.NumberFormat = exports.BooleanNormalizer = exports.BooleanValidator = exports.NotBlankValidator = exports.SizeValidator = exports.NotEmptyValidator = exports.IsNullOrUndefinedValidator = exports.NotNullOrUndefinedValidator = exports.StringNormalizer = exports.StringValidator = exports.NextStringValidator = exports.StringValidatorBase = exports.AnyValidator = exports.JsonBigInt = exports.JsonSet = exports.SetValidator = exports.JsonMap = exports.MapNormalizer = exports.MapValidator = exports.WhenGroup = exports.WhenGroupValidator = exports.Conditional = exports.IfValidator = exports.AnyOfValidator = exports.OneOfValidator = exports.CompositionValidator = exports.CheckValidator = exports.ArrayNormalizer = exports.ArrayValidator = exports.ValidatorFnWrapper = exports.defaultViolations = exports.ValidatorType = exports.Groups = exports.Group = exports.SizeViolation = exports.MaxViolation = exports.MinViolation = exports.OneOfMismatch = exports.PatternViolation = exports.HasValueViolation = exports.ErrorViolation = exports.EnumMismatch = exports.TypeMismatch = exports.Violation = exports.ValidationError = exports.ValidationResult = exports.Validator = exports.ValidationContext = void 0; | ||
exports.JsonValidator = exports.IgnoreValidator = exports.ValueMapper = exports.RequiredValidator = exports.NullableValidator = exports.OptionalUndefinedValidator = exports.OptionalValidator = exports.PatternNormalizer = exports.PatternValidator = exports.DateValidator = exports.AllOfValidator = exports.HasValueValidator = exports.UuidValidator = exports.AssertTrueValidator = exports.EnumValidator = exports.MaxValidator = exports.MinValidator = exports.NumberNormalizer = exports.NumberValidator = exports.NextNumberValidator = void 0; | ||
exports.isNullOrUndefined = isNullOrUndefined; | ||
@@ -742,2 +742,18 @@ exports.isString = isString; | ||
exports.JsonSet = JsonSet; | ||
class JsonBigInt { | ||
value; | ||
constructor(value) { | ||
this.value = value; | ||
if (typeof value !== 'bigint') { | ||
throw new Error('Expected bigint, got ' + typeof value); | ||
} | ||
} | ||
valueOf() { | ||
return this.value; | ||
} | ||
toJSON() { | ||
return this.value.toString(10); | ||
} | ||
} | ||
exports.JsonBigInt = JsonBigInt; | ||
class AnyValidator extends Validator { | ||
@@ -974,2 +990,37 @@ validatePath(value, path, ctx) { | ||
exports.NumberValidatorBase = NumberValidatorBase; | ||
const bigIntFormat = /^-?[0-9]+$/; | ||
class JsonBigIntValidator extends Validator { | ||
constructor() { | ||
super(); | ||
Object.freeze(this); | ||
} | ||
validatePath(value, path, ctx) { | ||
const valueType = typeof value; | ||
switch (valueType) { | ||
case 'bigint': | ||
return Promise.resolve(new JsonBigInt(value)); | ||
case 'number': | ||
try { | ||
return Promise.resolve(new JsonBigInt(BigInt(value))); | ||
} | ||
catch (e) { | ||
return Promise.reject(new TypeMismatch(path, 'integer', value)); | ||
} | ||
case 'string': | ||
if (value.match(bigIntFormat)) { | ||
return Promise.resolve(new JsonBigInt(BigInt(value))); | ||
} | ||
else { | ||
return Promise.reject(new TypeMismatch(path, bigIntFormat.toString(), value)); | ||
} | ||
case 'object': | ||
if (value instanceof JsonBigInt) { | ||
return Promise.resolve(value); | ||
} | ||
break; | ||
} | ||
return Promise.reject(new TypeMismatch(path, 'JsonBigInt, bigint or integer as number or string', value)); | ||
} | ||
} | ||
exports.JsonBigIntValidator = JsonBigIntValidator; | ||
class NextNumberValidator extends NumberValidatorBase { | ||
@@ -976,0 +1027,0 @@ firstValidator; |
import { SchemaValidator, SchemaModel } from './schema.js'; | ||
import { Path } from '@finnair/path'; | ||
import { IgnoreValidator, ArrayNormalizer, AnyValidator, StringValidator, StringNormalizer, NotNullOrUndefinedValidator, IsNullOrUndefinedValidator, NotEmptyValidator, NotBlankValidator, ValueMapper, BooleanValidator, NumberValidator, NumberNormalizer, DateValidator, ValidatorFn, ValidatorFnWrapper, MappingFn, Validator, CheckValidator, OptionalValidator, AssertTrue, IfValidator, GroupOrName, WhenGroupValidator, AssertTrueValidator, PatternValidator, PatternNormalizer, BooleanNormalizer, MinValidator, MaxValidator, MapValidator, MapNormalizer, ArrayValidator, SizeValidator, AnyOfValidator, OneOfValidator, EnumValidator, HasValueValidator, JsonValidator, RequiredValidator, SetValidator, UuidValidator, VType, CompositionParameters, OptionalUndefinedValidator, NullableValidator } from './validators.js'; | ||
import { IgnoreValidator, ArrayNormalizer, AnyValidator, StringValidator, StringNormalizer, NotNullOrUndefinedValidator, IsNullOrUndefinedValidator, NotEmptyValidator, NotBlankValidator, ValueMapper, BooleanValidator, NumberValidator, NumberNormalizer, DateValidator, ValidatorFn, ValidatorFnWrapper, MappingFn, Validator, CheckValidator, OptionalValidator, AssertTrue, IfValidator, GroupOrName, WhenGroupValidator, AssertTrueValidator, PatternValidator, PatternNormalizer, BooleanNormalizer, MinValidator, MaxValidator, MapValidator, MapNormalizer, ArrayValidator, SizeValidator, AnyOfValidator, OneOfValidator, EnumValidator, HasValueValidator, JsonValidator, RequiredValidator, SetValidator, UuidValidator, VType, CompositionParameters, OptionalUndefinedValidator, NullableValidator, JsonBigIntValidator } from './validators.js'; | ||
import { ObjectModel, ObjectValidator, ObjectNormalizer } from './objectValidator.js'; | ||
@@ -58,2 +58,3 @@ import { ObjectValidatorBuilder } from './objectValidatorBuilder.js'; | ||
max: (max: number, inclusive?: boolean) => MaxValidator; | ||
jsonBigInt: () => JsonBigIntValidator; | ||
object: <T, I = T>(model: ObjectModel<T, I>) => ObjectValidator<T, I>; | ||
@@ -60,0 +61,0 @@ objectType: () => ObjectValidatorBuilder<unknown, unknown, unknown, unknown>; |
import { SchemaValidator } from './schema.js'; | ||
import { IgnoreValidator, ArrayNormalizer, AnyValidator, StringValidator, StringNormalizer, NotNullOrUndefinedValidator, IsNullOrUndefinedValidator, NotEmptyValidator, NotBlankValidator, ValueMapper, isNullOrUndefined, BooleanValidator, NumberValidator, NumberFormat, NumberNormalizer, DateValidator, ValidatorType, ValidatorFnWrapper, CheckValidator, OptionalValidator, IfValidator, Conditional, WhenGroupValidator, WhenGroup, AssertTrueValidator, PatternValidator, PatternNormalizer, BooleanNormalizer, MinValidator, MaxValidator, MapValidator, MapNormalizer, ArrayValidator, SizeValidator, AllOfValidator, AnyOfValidator, OneOfValidator, EnumValidator, HasValueValidator, JsonValidator, RequiredValidator, SetValidator, UuidValidator, maybeCompositionOf, OptionalUndefinedValidator, NullableValidator, } from './validators.js'; | ||
import { IgnoreValidator, ArrayNormalizer, AnyValidator, StringValidator, StringNormalizer, NotNullOrUndefinedValidator, IsNullOrUndefinedValidator, NotEmptyValidator, NotBlankValidator, ValueMapper, isNullOrUndefined, BooleanValidator, NumberValidator, NumberFormat, NumberNormalizer, DateValidator, ValidatorType, ValidatorFnWrapper, CheckValidator, OptionalValidator, IfValidator, Conditional, WhenGroupValidator, WhenGroup, AssertTrueValidator, PatternValidator, PatternNormalizer, BooleanNormalizer, MinValidator, MaxValidator, MapValidator, MapNormalizer, ArrayValidator, SizeValidator, AllOfValidator, AnyOfValidator, OneOfValidator, EnumValidator, HasValueValidator, JsonValidator, RequiredValidator, SetValidator, UuidValidator, maybeCompositionOf, OptionalUndefinedValidator, NullableValidator, JsonBigIntValidator, } from './validators.js'; | ||
import { ObjectValidator, ObjectNormalizer } from './objectValidator.js'; | ||
import { ObjectValidatorBuilder } from './objectValidatorBuilder.js'; | ||
const AllOfConstructor = (...validators) => new AllOfValidator(validators); | ||
const ignoreValidator = new IgnoreValidator(), stringValidator = new StringValidator(), toStringValidator = new StringNormalizer(), nullOrUndefinedValidator = new IsNullOrUndefinedValidator(), notBlankValidator = new NotBlankValidator(), emptyToNullValidator = new ValueMapper((value) => (isNullOrUndefined(value) || value === '' ? null : value)), emptyToUndefinedValidator = new ValueMapper((value) => (isNullOrUndefined(value) || value === '' ? undefined : value)), undefinedToNullValidator = new ValueMapper((value) => (value === undefined ? null : value)), booleanValidator = new BooleanValidator(), numberValidator = new NumberValidator(NumberFormat.number), toNumberValidator = new NumberNormalizer(NumberFormat.number), integerValidator = new NumberValidator(NumberFormat.integer), toIntegerValidator = new NumberNormalizer(NumberFormat.integer), dateValidator = new DateValidator(ValidatorType.Date); | ||
const ignoreValidator = new IgnoreValidator(), stringValidator = new StringValidator(), toStringValidator = new StringNormalizer(), nullOrUndefinedValidator = new IsNullOrUndefinedValidator(), notBlankValidator = new NotBlankValidator(), emptyToNullValidator = new ValueMapper((value) => (isNullOrUndefined(value) || value === '' ? null : value)), emptyToUndefinedValidator = new ValueMapper((value) => (isNullOrUndefined(value) || value === '' ? undefined : value)), undefinedToNullValidator = new ValueMapper((value) => (value === undefined ? null : value)), booleanValidator = new BooleanValidator(), numberValidator = new NumberValidator(NumberFormat.number), toNumberValidator = new NumberNormalizer(NumberFormat.number), integerValidator = new NumberValidator(NumberFormat.integer), jsonBigIntValidator = new JsonBigIntValidator(), toIntegerValidator = new NumberNormalizer(NumberFormat.integer), dateValidator = new DateValidator(ValidatorType.Date); | ||
export const V = { | ||
@@ -49,2 +49,3 @@ fn: (fn, type) => new ValidatorFnWrapper(fn, type), | ||
max: (max, inclusive = true) => new MaxValidator(max, inclusive), | ||
jsonBigInt: () => jsonBigIntValidator, | ||
object: (model) => new ObjectValidator(model), | ||
@@ -51,0 +52,0 @@ objectType: () => new ObjectValidatorBuilder(), |
@@ -264,2 +264,8 @@ import { Path } from '@finnair/path'; | ||
} | ||
export declare class JsonBigInt { | ||
readonly value: bigint; | ||
constructor(value: bigint); | ||
valueOf(): bigint; | ||
toJSON(): string; | ||
} | ||
export declare class AnyValidator<InOut = any> extends Validator<InOut> { | ||
@@ -331,2 +337,6 @@ validatePath(value: InOut, path: Path, ctx: ValidationContext): PromiseLike<InOut>; | ||
} | ||
export declare class JsonBigIntValidator extends Validator<JsonBigInt, any> { | ||
constructor(); | ||
validatePath(value: any, path: Path, ctx: ValidationContext): PromiseLike<JsonBigInt>; | ||
} | ||
export declare class NextNumberValidator<In> extends NumberValidatorBase<In> { | ||
@@ -333,0 +343,0 @@ readonly firstValidator: Validator<number, any>; |
@@ -694,2 +694,17 @@ import { default as deepEqual } from 'fast-deep-equal'; | ||
} | ||
export class JsonBigInt { | ||
value; | ||
constructor(value) { | ||
this.value = value; | ||
if (typeof value !== 'bigint') { | ||
throw new Error('Expected bigint, got ' + typeof value); | ||
} | ||
} | ||
valueOf() { | ||
return this.value; | ||
} | ||
toJSON() { | ||
return this.value.toString(10); | ||
} | ||
} | ||
export class AnyValidator extends Validator { | ||
@@ -913,2 +928,36 @@ validatePath(value, path, ctx) { | ||
} | ||
const bigIntFormat = /^-?[0-9]+$/; | ||
export class JsonBigIntValidator extends Validator { | ||
constructor() { | ||
super(); | ||
Object.freeze(this); | ||
} | ||
validatePath(value, path, ctx) { | ||
const valueType = typeof value; | ||
switch (valueType) { | ||
case 'bigint': | ||
return Promise.resolve(new JsonBigInt(value)); | ||
case 'number': | ||
try { | ||
return Promise.resolve(new JsonBigInt(BigInt(value))); | ||
} | ||
catch (e) { | ||
return Promise.reject(new TypeMismatch(path, 'integer', value)); | ||
} | ||
case 'string': | ||
if (value.match(bigIntFormat)) { | ||
return Promise.resolve(new JsonBigInt(BigInt(value))); | ||
} | ||
else { | ||
return Promise.reject(new TypeMismatch(path, bigIntFormat.toString(), value)); | ||
} | ||
case 'object': | ||
if (value instanceof JsonBigInt) { | ||
return Promise.resolve(value); | ||
} | ||
break; | ||
} | ||
return Promise.reject(new TypeMismatch(path, 'JsonBigInt, bigint or integer as number or string', value)); | ||
} | ||
} | ||
export class NextNumberValidator extends NumberValidatorBase { | ||
@@ -915,0 +964,0 @@ firstValidator; |
{ | ||
"name": "@finnair/v-validation", | ||
"version": "7.0.0-alpha.9", | ||
"version": "7.0.0", | ||
"private": false, | ||
@@ -48,8 +48,8 @@ "description": "V-validation core package", | ||
"peerDependencies": { | ||
"@finnair/path": ">=6" | ||
"@finnair/path": ">=7" | ||
}, | ||
"devDependencies": { | ||
"@finnair/path": "^7.0.0-alpha.9" | ||
"@finnair/path": "^7.0.0" | ||
}, | ||
"gitHead": "d1b48ecef3244ae6602e45bcf1a726fd1875b475" | ||
"gitHead": "19e89e62936366d23fbff76fbb61abe91d943222" | ||
} |
@@ -26,3 +26,3 @@ ![CI](https://github.com/finnair/v-validation/workflows/CI/badge.svg?branch=master) | ||
## Major Changes Comming in Version 7 | ||
## Major Changes in Version 7 | ||
### New Features | ||
@@ -33,15 +33,16 @@ * **Typing**: Validators may have a specific input and especially output type. | ||
* Direct, chainable support for most used "next" validation rules, e.g. `V.number().min(1).max(2)`: | ||
* `V.string()` supports `notEmpty`, `notBlank`, `pattern` and `size`. | ||
* `V.string()` supports `notEmpty`, `notBlank`, `pattern` and `size`, | ||
* `V.number()` supports `min`, `max` and `between`. | ||
* Use `Validator#getValid(input)` to get valid a valid value or an exception directly | ||
* Use `Validator#getValid(input)` to get valid a valid value or an exception directly. | ||
* New strictly typed "optional" validators: | ||
* `V.optionalStrict<T>(validator: Validator<T>)`: `undefined | T` - `V.optional` allows also null | ||
* `V.nullable<T>(validator: Validator<T>)`: `null | T` | ||
* `V.optionalProperties<K, V>(keys: Validator<K>, values: Validator<V>)`: `Partial<Record<Key, Value>>` | ||
* `V.optionalStrict<T>(validator: Validator<T>)`: `undefined | T` - `V.optional` allows also null, | ||
* `V.nullable<T>(validator: Validator<T>)`: `null | T`, | ||
* `V.optionalProperties<K, V>(keys: Validator<K>, values: Validator<V>)`: `Partial<Record<Key, Value>>`. | ||
* `JsonBigInt`: `BigInt` wrapper with `toJSON()` serialization as `string`. | ||
### Breaking changes: | ||
* `V.string()` and some other validators do not support String object as input any more. | ||
* `isString()` function doesn't support String object any more | ||
* `isString()` function doesn't support String object any more. | ||
* `V.number()` does not support Number object as input any more. | ||
* `V.allOf()` requires that all results match | ||
* `V.allOf()` requires that all results match. | ||
* Validators that accept multiple subvalidators (`V.optional`, `V.required`, `V.check`, `V.if`, `V.whenGroup`, `V.json` and `ObjectModel#next`) are combined using `V.compositionOf` instead of `V.allOf` as composition makes more sense in general. However, if there are multiple parents with next validators, those are still combined with `V.allOf` as they are not aware of each other. | ||
@@ -51,5 +52,5 @@ * `V.if` does not support "fall through" any more but rejects with NoMatchingCondition if no condition matches. Use `.else(V.any())` if "fall through" is desirable. | ||
* More straightforward internal architecture: | ||
* Internal Validator#validatePath returns now a Promise of valid value or reject of Violation(s) directly instead of ValidationResult | ||
* Custom SyncPromise is removed in favor of Promise.resolve and reject. | ||
* ValidatorContext no longer has `success`, `successPromise`, `failurePromise` and `promise` functions - use `Promise.resolve(value)` or `Promise.reject(new Violation(...))` with single violation or an array of violations. | ||
* internal Validator#validatePath returns now a Promise of valid value or reject of Violation(s) directly instead of ValidationResult, | ||
* custom SyncPromise is removed in favor of Promise.resolve and reject, | ||
* `ValidatorContext` no longer has `success`, `successPromise`, `failurePromise` and `promise` functions - use `Promise.resolve(value)` or `Promise.reject(new Violation(...))` with single violation or an array of violations. | ||
* `V.mapType`, `V.toMapType` and `V.setType` now require `jsonSafe` boolean parameter for typing: JsonMap/JsonSet (true) or plain Map/Set (false). | ||
@@ -265,3 +266,3 @@ | ||
All built-in validators (except `V.shema`) have input and output types. Typed ObjectValidators can be built with `V.objectType()`. | ||
All built-in validators (except `V.schema`) have input and output types. Typed ObjectValidators can be built with `V.objectType()`. | ||
Since inferred types tend to get quite long and hard to read, you can also combine them with hand-written types. | ||
@@ -272,5 +273,5 @@ | ||
Use `VInheritableType<typeof objectValidator> to get the inheritable type of `objectValidator: ObjectValidator<LocalType, Inheritabletype>`. | ||
Use `VInheritableType<typeof objectValidator> to get the inheritable type of `objectValidator: ObjectValidator<LocalType, InheritableType>`. | ||
LocalType and Inheritabletype will only differ when `localProperties` or `localNext` are used. The most obvious use case for this is a class hierarcy with discriminator property to denote a specific type. | ||
LocalType and InheritableType will only differ when `localProperties` or `localNext` are used. The most obvious use case for this is a class hierarcy with discriminator property to denote a specific type. | ||
@@ -292,3 +293,3 @@ *NOTE: `V.schema` doesn't yet support typing.* | ||
```typescript | ||
interface MyInterface{ | ||
interface MyInterface { | ||
//... | ||
@@ -312,9 +313,7 @@ } | ||
- All validators have [`Validator.next`](#next) function to chain validator rules,. | ||
- `compositionOf` - validators are run one after another against the (current) converted value (a shortcut for [`Validator.next`](#next)) | ||
- `allOf` - value must satisfy all the validators | ||
- validators are run in parallel and the results are combined | ||
- all the validators must return the same value (deepEquals) | ||
- `anyOf` - at least one of the validators must match | ||
- `oneOf` - exactly one validator must match while others should return false | ||
- All validators have [`Validator.next`](#next) function to chain validator rules. | ||
- `compositionOf` - validators are run one after another against the (current) converted value (a shortcut for [`Validator.next`](#next)). | ||
- `allOf` - the input value must satisfy all the validators. Validators are run in parallel and must return the same value (deepEquals). | ||
- `anyOf` - at least one of the validators must match. | ||
- `oneOf` - exactly one validator must match while others should return false. | ||
@@ -328,6 +327,8 @@ ## <a name="object">V.object</a> | ||
3. references to parent model(s), | ||
4. local (non-inheritable) properties and | ||
5. next validator for cross-property rules | ||
6. local next for non-inheritable mapping | ||
4. local (non-inheritable) properties, | ||
5. next validator for cross-property rules and | ||
6. local next for non-inheritable mapping. | ||
Note that `V.object` is essentially deprecated. Please use `V.objectType` to create properly typed object validators. | ||
### Named Properties | ||
@@ -602,3 +603,3 @@ | ||
| --------------------------------- | ------------------------------------------ | | ||
| ignoreUnknownProperties?: boolean | Unknown properties allowed by default.\* | | ||
| ignoreUnknownProperties?: boolean | Unknown properties allowed by default\* | | ||
| ignoreUnknownEnumValues?: boolean | Unknown enum values allowed by default | | ||
@@ -695,2 +696,3 @@ | warnLogger?: WarnLogger | A reporter function for ignored Violations | | ||
| max | max: number, inclusive = true | Asserts that numeric input is less than or equal (if inclusive = true) than `max`. | | ||
| jsonBigInt | | Converts bigints, integer numbers and strings to `JsonBigInt` wrapper for `BigInt` with `toJSON()` serialization as string. | | ||
| date | | Reqruires a valid date. Converts string to Date. | | ||
@@ -697,0 +699,0 @@ | enum | enumType: object, name: string | Requires that the input is one of given enumType. Name of the enum provided for error message. | |
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
318784
5586
0
760