@finnair/v-validation
Advanced tools
Comparing version 7.0.0-alpha.8 to 7.0.0-alpha.9
@@ -6,2 +6,8 @@ # Change Log | ||
# [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) | ||
### Features | ||
- V.mapType, V.toMapType and V.setType typing ([#123](https://github.com/finnair/v-validation/issues/123)) ([ed7780c](https://github.com/finnair/v-validation/commit/ed7780c284fb8d41157cf33fa54f6984275c6283)) | ||
# [7.0.0-alpha.8](https://github.com/finnair/v-validation/compare/v7.0.0-alpha.7...v7.0.0-alpha.8) (2024-11-21) | ||
@@ -8,0 +14,0 @@ |
@@ -63,6 +63,6 @@ import { SchemaValidator, SchemaModel } from './schema.js'; | ||
/** WARN: Objects as Map keys use identity hash/equals, i.e. === */ | ||
mapType: (keys: Validator, values: Validator, jsonSafeMap?: boolean) => MapValidator<unknown, unknown>; | ||
mapType: <K, V, E extends boolean>(keys: Validator<K>, values: Validator<V>, jsonSafeMap: E) => MapValidator<K, V, E>; | ||
/** WARN: Objects as Map keys use identity hash/equals, i.e. === */ | ||
toMapType: (keys: Validator, values: Validator) => MapNormalizer<unknown, unknown>; | ||
setType: (values: Validator, jsonSafeSet?: boolean) => SetValidator<unknown>; | ||
toMapType: <K, V, E extends boolean>(keys: Validator<K>, values: Validator<V>, jsonSafeMap: E) => MapNormalizer<K, V, E>; | ||
setType: <T, E extends boolean>(values: Validator<T>, jsonSafeSet: E) => SetValidator<T, E>; | ||
nullTo: <Out extends string | number | bigint | boolean | symbol, In = unknown>(defaultValue: Out) => ValueMapper<Out | In, In>; | ||
@@ -69,0 +69,0 @@ nullToObject: <In>() => ValueMapper<{} | In, In>; |
@@ -57,6 +57,6 @@ "use strict"; | ||
/** WARN: Objects as Map keys use identity hash/equals, i.e. === */ | ||
mapType: (keys, values, jsonSafeMap = true) => new validators_js_1.MapValidator(keys, values, jsonSafeMap), | ||
mapType: (keys, values, jsonSafeMap) => new validators_js_1.MapValidator(keys, values, jsonSafeMap), | ||
/** WARN: Objects as Map keys use identity hash/equals, i.e. === */ | ||
toMapType: (keys, values) => new validators_js_1.MapNormalizer(keys, values), | ||
setType: (values, jsonSafeSet = true) => new validators_js_1.SetValidator(values, jsonSafeSet), | ||
toMapType: (keys, values, jsonSafeMap) => new validators_js_1.MapNormalizer(keys, values, jsonSafeMap), | ||
setType: (values, jsonSafeSet) => new validators_js_1.SetValidator(values, jsonSafeSet), | ||
nullTo: (defaultValue) => new validators_js_1.ValueMapper((value) => ((0, validators_js_1.isNullOrUndefined)(value) ? defaultValue : value)), | ||
@@ -63,0 +63,0 @@ nullToObject: () => new validators_js_1.ValueMapper(value => ((0, validators_js_1.isNullOrUndefined)(value) ? {} : value)), |
@@ -239,25 +239,25 @@ import { Path } from '@finnair/path'; | ||
} | ||
export declare class MapValidator<K extends unknown, V extends unknown> extends Validator<Map<K, V>> { | ||
export declare class MapValidator<K = unknown, V = unknown, E extends boolean = true> extends Validator<E extends true ? JsonMap<K, V> : Map<K, V>> { | ||
readonly keys: Validator<K>; | ||
readonly values: Validator<V>; | ||
readonly jsonSafeMap: boolean; | ||
constructor(keys: Validator<K>, values: Validator<V>, jsonSafeMap: boolean); | ||
validatePath(value: unknown, path: Path, ctx: ValidationContext): PromiseLike<Map<K, V>>; | ||
readonly jsonSafeMap: E; | ||
constructor(keys: Validator<K>, values: Validator<V>, jsonSafeMap: E); | ||
validatePath(value: unknown, path: Path, ctx: ValidationContext): PromiseLike<E extends true ? JsonMap<K, V> : Map<K, V>>; | ||
} | ||
export declare class MapNormalizer<K = unknown, V = unknown> extends MapValidator<K, V> { | ||
constructor(keys: Validator<K>, values: Validator<V>, jsonSafeMap?: boolean); | ||
validatePath(value: unknown, path: Path, ctx: ValidationContext): PromiseLike<Map<K, V>>; | ||
export declare class MapNormalizer<K = unknown, V = unknown, E extends boolean = true> extends MapValidator<K, V, E> { | ||
constructor(keys: Validator<K>, values: Validator<V>, jsonSafeMap: E); | ||
validatePath(value: unknown, path: Path, ctx: ValidationContext): PromiseLike<E extends true ? JsonMap<K, V> : Map<K, V>>; | ||
} | ||
export declare class JsonMap<K, V> extends Map<K, V> { | ||
constructor(params?: any); | ||
constructor(entries?: readonly (readonly [K, V])[] | null); | ||
toJSON(): [K, V][]; | ||
} | ||
export declare class SetValidator<T = unknown> extends Validator<Set<T>> { | ||
export declare class SetValidator<T = unknown, E extends boolean = true> extends Validator<E extends true ? JsonSet<T> : Set<T>> { | ||
readonly values: Validator<T>; | ||
readonly jsonSafeSet: boolean; | ||
constructor(values: Validator<T>, jsonSafeSet: boolean); | ||
validatePath(value: unknown, path: Path, ctx: ValidationContext): PromiseLike<Set<T>>; | ||
readonly jsonSafeSet: E; | ||
constructor(values: Validator<T>, jsonSafeSet: E); | ||
validatePath(value: unknown, path: Path, ctx: ValidationContext): PromiseLike<E extends true ? JsonSet<T> : Set<T>>; | ||
} | ||
export declare class JsonSet<K> extends Set<K> { | ||
constructor(params?: any); | ||
constructor(values?: readonly K[] | null); | ||
toJSON(): K[]; | ||
@@ -264,0 +264,0 @@ } |
@@ -652,3 +652,3 @@ "use strict"; | ||
class MapNormalizer extends MapValidator { | ||
constructor(keys, values, jsonSafeMap = true) { | ||
constructor(keys, values, jsonSafeMap) { | ||
super(keys, values, jsonSafeMap); | ||
@@ -687,4 +687,4 @@ } | ||
class JsonMap extends Map { | ||
constructor(params) { | ||
super(params); | ||
constructor(entries) { | ||
super(entries); | ||
} | ||
@@ -735,4 +735,4 @@ toJSON() { | ||
class JsonSet extends Set { | ||
constructor(params) { | ||
super(params); | ||
constructor(values) { | ||
super(values); | ||
} | ||
@@ -739,0 +739,0 @@ toJSON() { |
@@ -63,6 +63,6 @@ import { SchemaValidator, SchemaModel } from './schema.js'; | ||
/** WARN: Objects as Map keys use identity hash/equals, i.e. === */ | ||
mapType: (keys: Validator, values: Validator, jsonSafeMap?: boolean) => MapValidator<unknown, unknown>; | ||
mapType: <K, V, E extends boolean>(keys: Validator<K>, values: Validator<V>, jsonSafeMap: E) => MapValidator<K, V, E>; | ||
/** WARN: Objects as Map keys use identity hash/equals, i.e. === */ | ||
toMapType: (keys: Validator, values: Validator) => MapNormalizer<unknown, unknown>; | ||
setType: (values: Validator, jsonSafeSet?: boolean) => SetValidator<unknown>; | ||
toMapType: <K, V, E extends boolean>(keys: Validator<K>, values: Validator<V>, jsonSafeMap: E) => MapNormalizer<K, V, E>; | ||
setType: <T, E extends boolean>(values: Validator<T>, jsonSafeSet: E) => SetValidator<T, E>; | ||
nullTo: <Out extends string | number | bigint | boolean | symbol, In = unknown>(defaultValue: Out) => ValueMapper<Out | In, In>; | ||
@@ -69,0 +69,0 @@ nullToObject: <In>() => ValueMapper<{} | In, In>; |
@@ -54,6 +54,6 @@ import { SchemaValidator } from './schema.js'; | ||
/** WARN: Objects as Map keys use identity hash/equals, i.e. === */ | ||
mapType: (keys, values, jsonSafeMap = true) => new MapValidator(keys, values, jsonSafeMap), | ||
mapType: (keys, values, jsonSafeMap) => new MapValidator(keys, values, jsonSafeMap), | ||
/** WARN: Objects as Map keys use identity hash/equals, i.e. === */ | ||
toMapType: (keys, values) => new MapNormalizer(keys, values), | ||
setType: (values, jsonSafeSet = true) => new SetValidator(values, jsonSafeSet), | ||
toMapType: (keys, values, jsonSafeMap) => new MapNormalizer(keys, values, jsonSafeMap), | ||
setType: (values, jsonSafeSet) => new SetValidator(values, jsonSafeSet), | ||
nullTo: (defaultValue) => new ValueMapper((value) => (isNullOrUndefined(value) ? defaultValue : value)), | ||
@@ -60,0 +60,0 @@ nullToObject: () => new ValueMapper(value => (isNullOrUndefined(value) ? {} : value)), |
@@ -239,25 +239,25 @@ import { Path } from '@finnair/path'; | ||
} | ||
export declare class MapValidator<K extends unknown, V extends unknown> extends Validator<Map<K, V>> { | ||
export declare class MapValidator<K = unknown, V = unknown, E extends boolean = true> extends Validator<E extends true ? JsonMap<K, V> : Map<K, V>> { | ||
readonly keys: Validator<K>; | ||
readonly values: Validator<V>; | ||
readonly jsonSafeMap: boolean; | ||
constructor(keys: Validator<K>, values: Validator<V>, jsonSafeMap: boolean); | ||
validatePath(value: unknown, path: Path, ctx: ValidationContext): PromiseLike<Map<K, V>>; | ||
readonly jsonSafeMap: E; | ||
constructor(keys: Validator<K>, values: Validator<V>, jsonSafeMap: E); | ||
validatePath(value: unknown, path: Path, ctx: ValidationContext): PromiseLike<E extends true ? JsonMap<K, V> : Map<K, V>>; | ||
} | ||
export declare class MapNormalizer<K = unknown, V = unknown> extends MapValidator<K, V> { | ||
constructor(keys: Validator<K>, values: Validator<V>, jsonSafeMap?: boolean); | ||
validatePath(value: unknown, path: Path, ctx: ValidationContext): PromiseLike<Map<K, V>>; | ||
export declare class MapNormalizer<K = unknown, V = unknown, E extends boolean = true> extends MapValidator<K, V, E> { | ||
constructor(keys: Validator<K>, values: Validator<V>, jsonSafeMap: E); | ||
validatePath(value: unknown, path: Path, ctx: ValidationContext): PromiseLike<E extends true ? JsonMap<K, V> : Map<K, V>>; | ||
} | ||
export declare class JsonMap<K, V> extends Map<K, V> { | ||
constructor(params?: any); | ||
constructor(entries?: readonly (readonly [K, V])[] | null); | ||
toJSON(): [K, V][]; | ||
} | ||
export declare class SetValidator<T = unknown> extends Validator<Set<T>> { | ||
export declare class SetValidator<T = unknown, E extends boolean = true> extends Validator<E extends true ? JsonSet<T> : Set<T>> { | ||
readonly values: Validator<T>; | ||
readonly jsonSafeSet: boolean; | ||
constructor(values: Validator<T>, jsonSafeSet: boolean); | ||
validatePath(value: unknown, path: Path, ctx: ValidationContext): PromiseLike<Set<T>>; | ||
readonly jsonSafeSet: E; | ||
constructor(values: Validator<T>, jsonSafeSet: E); | ||
validatePath(value: unknown, path: Path, ctx: ValidationContext): PromiseLike<E extends true ? JsonSet<T> : Set<T>>; | ||
} | ||
export declare class JsonSet<K> extends Set<K> { | ||
constructor(params?: any); | ||
constructor(values?: readonly K[] | null); | ||
toJSON(): K[]; | ||
@@ -264,0 +264,0 @@ } |
@@ -609,3 +609,3 @@ import { default as deepEqual } from 'fast-deep-equal'; | ||
export class MapNormalizer extends MapValidator { | ||
constructor(keys, values, jsonSafeMap = true) { | ||
constructor(keys, values, jsonSafeMap) { | ||
super(keys, values, jsonSafeMap); | ||
@@ -643,4 +643,4 @@ } | ||
export class JsonMap extends Map { | ||
constructor(params) { | ||
super(params); | ||
constructor(entries) { | ||
super(entries); | ||
} | ||
@@ -689,4 +689,4 @@ toJSON() { | ||
export class JsonSet extends Set { | ||
constructor(params) { | ||
super(params); | ||
constructor(values) { | ||
super(values); | ||
} | ||
@@ -693,0 +693,0 @@ toJSON() { |
{ | ||
"name": "@finnair/v-validation", | ||
"version": "7.0.0-alpha.8", | ||
"version": "7.0.0-alpha.9", | ||
"private": false, | ||
@@ -51,5 +51,5 @@ "description": "V-validation core package", | ||
"devDependencies": { | ||
"@finnair/path": "^7.0.0-alpha.8" | ||
"@finnair/path": "^7.0.0-alpha.9" | ||
}, | ||
"gitHead": "ac7c2ffa732c06f485d51656fa538ae95457611f" | ||
"gitHead": "d1b48ecef3244ae6602e45bcf1a726fd1875b475" | ||
} |
@@ -34,3 +34,7 @@ ![CI](https://github.com/finnair/v-validation/workflows/CI/badge.svg?branch=master) | ||
* `V.number()` supports `min`, `max` and `between`. | ||
* Use `Validator#validateValue` 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>>` | ||
@@ -49,2 +53,3 @@ ### Breaking changes: | ||
* 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). | ||
@@ -87,3 +92,3 @@ ## Show Me the Code! | ||
Optional `Vmoment` (`@finnair/v-validation-moment`) extension uses custom Moment extensions to support full JSON roundtrip with strict validation. | ||
Optional `Vmoment` (`@finnair/v-validation-moment`) extension uses custom Moment extensions to support full JSON roundtrip with strict validation. *NOTE: As Moment is now a legacy project, we recommend using Luxon with `@finnair/v-validation-luxon` instead.* | ||
@@ -98,2 +103,11 @@ ```typescript | ||
Optional `Vluxon` (`@finnair/v-validation-luxon`) extension uses custom DateTime wrapper to support full JSON roundtrip with strict validation: | ||
```typescript | ||
const luxonDate: LocalDateLuxon = await Vluxon.localDate().getValid('2020-03-05'); | ||
JSON.stringify(luxonDate); | ||
// "2020-03-05" | ||
``` | ||
Validators are effectively immutable objects/functions that can be combined to form more complex models. | ||
@@ -252,3 +266,3 @@ | ||
All built-in validators have input and output types. Typed ObjectValidators can be built with `V.objectType()`. | ||
All built-in validators (except `V.shema`) 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. | ||
@@ -261,2 +275,6 @@ | ||
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. | ||
*NOTE: `V.schema` doesn't yet support typing.* | ||
### Type Guards | ||
@@ -548,10 +566,5 @@ | ||
const keys = V.string(); | ||
const values = V.any(); | ||
const myMap = V.toMapType(keys, values); // keyValidator, valueValidator, jsonSafeMap? | ||
const map = ( | ||
await myMap.validate([ | ||
['key1', 'value1'], | ||
['key2', 'value2'], | ||
]) | ||
).getValue() as Map; | ||
const values = V.string(); | ||
const myMap = V.toMapType(keys, values, true); // keyValidator, valueValidator, jsonSafeMap | ||
const map = await myMap.getValid([['key1', 'value1'], ['key2', 'value2']]) satisfies JsonMap<string, string>; | ||
@@ -561,6 +574,22 @@ JSON.stringify(map); | ||
// Or without array conversion and JSON support: | ||
// Or plain Map without JSON serialization support: | ||
V.mapType(keys, values, false); | ||
``` | ||
## Set | ||
`V` supports JavaScript Sets with a custom extension for JSON serialization. | ||
```typescript | ||
const values = V.string(); | ||
const setValidator = V.setType(values, true); // valueValidator, jsonSafeSet | ||
const set = await setValidator.getValid(['value1', 'value2']) satisfies JsonSet<string>; | ||
JSON.stringify(set); | ||
// ["value1", "value2"] | ||
// Or plain Set without JSON serialization support: | ||
V.setType(values, false); // Validator<Set<string>> | ||
``` | ||
## Validator Options | ||
@@ -674,4 +703,5 @@ | ||
| optionalProperties | keys: Validator<Keys>, values: Validator<Values> | A shortcut for object with `additionalProperties`. Type: Partial<Record<Keys, Values>> | | ||
| mapType | keys: Validator, values: Validator, jsonSafeMap: boolean = true | [Map validator](#map) | | ||
| toMapType(keys, values) | keys: Validator, values: Validator | Converts an array-of-arrays representation of a Map into a JsonSafeMap instance. | | ||
| mapType | keys: Validator, values: Validator, jsonSafeMap: boolean | [Map validator](#map). JSON safe map (JsonMap) serializes into an array of [key, value]-arrays. | | ||
| toMapType(keys, values) | keys: Validator, values: Validator | Converts an array-of-arrays representation of a Map into a JsonMap instance. | | ||
| setType | values: Validator, jsonSafeMap: boolean | [Set validator](#set). JSON safe set (JsonSet) serializes into an array of values. | | ||
| array | ...items: Validator[] | [Array validator](#array) | | ||
@@ -678,0 +708,0 @@ | toArray | items: Validator | Converts undefined to an empty array and non-arrays to single-valued arrays. | |
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
313603
758