@evolu/common 8.3.0 → 8.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/Type.d.ts +65 -87
- package/dist/src/Type.d.ts.map +1 -1
- package/dist/src/Type.js +226 -136
- package/package.json +5 -5
- package/src/Type.ts +252 -166
package/dist/src/Type.d.ts
CHANGED
|
@@ -88,7 +88,7 @@ export interface Type<Name extends TypeName, in out Input, in out Output, Error
|
|
|
88
88
|
readonly fromUnknown: (value: unknown, options?: ValidationOptions) => Result<Output, Errors>;
|
|
89
89
|
/**
|
|
90
90
|
* Formats an error returned by `fromUnknown` or `from` as one human-readable
|
|
91
|
-
* message.
|
|
91
|
+
* message. Predefined Types use English; {@link localizeTypes} derives Types
|
|
92
92
|
* with localized formatters.
|
|
93
93
|
*
|
|
94
94
|
* Structural errors retain nested errors and their locations in the typed
|
|
@@ -376,8 +376,8 @@ export declare const assertType: <T extends TypeNode>(type: T, value: unknown) =
|
|
|
376
376
|
* Pass the Types used together in one localization scope and formatter maps
|
|
377
377
|
* keyed by locale. TypeScript infers every formatter required by the selected
|
|
378
378
|
* Types, including errors from nested structural Types and recursive Lazy
|
|
379
|
-
* Types. Every locale must provide the complete inferred formatter set;
|
|
380
|
-
*
|
|
379
|
+
* Types. Every locale must provide the complete inferred formatter set; missing
|
|
380
|
+
* and unrelated formatters are compile-time errors.
|
|
381
381
|
*
|
|
382
382
|
* The result preserves the locale names, selected Type names, and exact
|
|
383
383
|
* TypeScript types. A localized Type validates exactly like its source Type;
|
|
@@ -429,8 +429,8 @@ export declare const assertType: <T extends TypeNode>(type: T, value: unknown) =
|
|
|
429
429
|
*
|
|
430
430
|
* ### Supported locales
|
|
431
431
|
*
|
|
432
|
-
* English is built in; use {@link Type} directly for its default formatters.
|
|
433
|
-
*
|
|
432
|
+
* English is built in; use {@link Type} directly for its default formatters. The
|
|
433
|
+
* following additional locales are available:
|
|
434
434
|
*
|
|
435
435
|
* - Arabic (`ar`)
|
|
436
436
|
* - Bengali (`bn`)
|
|
@@ -738,9 +738,9 @@ export declare function createType<Name extends TypeName, ParentType extends Con
|
|
|
738
738
|
* canonicalize multiple parent representations, but it must be total and must
|
|
739
739
|
* not lose distinctions present in the Output domain.
|
|
740
740
|
*
|
|
741
|
-
* Transformation callbacks are Type construction code. Their successful
|
|
742
|
-
*
|
|
743
|
-
*
|
|
741
|
+
* Transformation callbacks are Type construction code. Their successful results
|
|
742
|
+
* are asserted against the declared boundary so a broken callback fails as a
|
|
743
|
+
* developer error rather than becoming a validation error. Like all
|
|
744
744
|
* Type-construction callbacks, they are trusted to follow their declared
|
|
745
745
|
* TypeScript types. A `Result<_, never>` callback is therefore trusted never to
|
|
746
746
|
* return an `Err`.
|
|
@@ -756,13 +756,7 @@ export declare function createType<Name extends TypeName, ParentType extends Con
|
|
|
756
756
|
* ### Example
|
|
757
757
|
*
|
|
758
758
|
* ```ts
|
|
759
|
-
* import {
|
|
760
|
-
* Boolean,
|
|
761
|
-
* literal,
|
|
762
|
-
* ok,
|
|
763
|
-
* transform,
|
|
764
|
-
* union,
|
|
765
|
-
* } from "@evolu/common";
|
|
759
|
+
* import { Boolean, literal, ok, transform, union } from "@evolu/common";
|
|
766
760
|
*
|
|
767
761
|
* const BooleanString = union(literal("false"), literal("true"));
|
|
768
762
|
* const BooleanFromString = transform(
|
|
@@ -1052,18 +1046,18 @@ interface ObjectTagOutputByName {
|
|
|
1052
1046
|
/**
|
|
1053
1047
|
* Realm-neutral {@link Type} trusting an object's reported tag.
|
|
1054
1048
|
*
|
|
1055
|
-
* Predefined built-
|
|
1056
|
-
* that trusted code does not forge their tags. They do not
|
|
1057
|
-
* internal slots. A custom tag refines the supplied Type and adds
|
|
1058
|
-
* evidence to its Output, so only a value validated by the resulting
|
|
1059
|
-
* accepted by its typed operations.
|
|
1049
|
+
* Predefined tags for JavaScript built-ins expose their native Output type
|
|
1050
|
+
* under the assumption that trusted code does not forge their tags. They do not
|
|
1051
|
+
* verify native internal slots. A custom tag refines the supplied Type and adds
|
|
1052
|
+
* nominal evidence to its Output, so only a value validated by the resulting
|
|
1053
|
+
* Type is accepted by its typed operations.
|
|
1060
1054
|
*
|
|
1061
|
-
* `Object.prototype.toString` recognizes legitimate built-ins from
|
|
1062
|
-
* realm, but any object can customize the result with
|
|
1063
|
-
* Types returned by this factory therefore classify
|
|
1064
|
-
* not security boundaries. Passing a forged
|
|
1065
|
-
* assumption of the predefined Type. Primitive
|
|
1066
|
-
* time.
|
|
1055
|
+
* `Object.prototype.toString` recognizes legitimate JavaScript built-ins from
|
|
1056
|
+
* another realm, but any object can customize the result with
|
|
1057
|
+
* `Symbol.toStringTag`. Types returned by this factory therefore classify
|
|
1058
|
+
* trusted values; they are not security boundaries. Passing a forged JavaScript
|
|
1059
|
+
* built-in tag violates the trust assumption of the predefined Type. Primitive
|
|
1060
|
+
* Outputs are rejected at compile time.
|
|
1067
1061
|
*
|
|
1068
1062
|
* ### Example
|
|
1069
1063
|
*
|
|
@@ -2234,8 +2228,8 @@ export type UrlSafeString = typeof UrlSafeString.Output;
|
|
|
2234
2228
|
/**
|
|
2235
2229
|
* Base64Url text without padding.
|
|
2236
2230
|
*
|
|
2237
|
-
*
|
|
2238
|
-
* {@link base64UrlToUint8Array}.
|
|
2231
|
+
* Convert bytes to Base64Url with {@link uint8ArrayToBase64Url} and convert
|
|
2232
|
+
* Base64Url to bytes with {@link base64UrlToUint8Array}.
|
|
2239
2233
|
*
|
|
2240
2234
|
* @group String
|
|
2241
2235
|
*/
|
|
@@ -2250,7 +2244,7 @@ export interface Base64UrlError extends TypeError<"Base64Url"> {
|
|
|
2250
2244
|
readonly value: string;
|
|
2251
2245
|
}
|
|
2252
2246
|
/**
|
|
2253
|
-
*
|
|
2247
|
+
* Converts bytes to {@link Base64Url}.
|
|
2254
2248
|
*
|
|
2255
2249
|
* ### Example
|
|
2256
2250
|
*
|
|
@@ -2266,7 +2260,7 @@ export interface Base64UrlError extends TypeError<"Base64Url"> {
|
|
|
2266
2260
|
*/
|
|
2267
2261
|
export declare const uint8ArrayToBase64Url: (bytes: Uint8Array) => Base64Url;
|
|
2268
2262
|
/**
|
|
2269
|
-
*
|
|
2263
|
+
* Converts {@link Base64Url} to bytes.
|
|
2270
2264
|
*
|
|
2271
2265
|
* ### Example
|
|
2272
2266
|
*
|
|
@@ -2551,7 +2545,9 @@ export interface Int64StringError extends TypeError<"Int64String"> {
|
|
|
2551
2545
|
* const result = Int64FromInt64String.fromUnknown("9223372036854775807");
|
|
2552
2546
|
*
|
|
2553
2547
|
* expectOk(result, 9223372036854775807n);
|
|
2554
|
-
* expect(Int64FromInt64String.to(result.value)).toBe(
|
|
2548
|
+
* expect(Int64FromInt64String.to(result.value)).toBe(
|
|
2549
|
+
* "9223372036854775807",
|
|
2550
|
+
* );
|
|
2555
2551
|
* ```
|
|
2556
2552
|
*
|
|
2557
2553
|
* @group Number
|
|
@@ -3732,7 +3728,8 @@ type PlainObjectError = ObjectError<Readonly<Record<never, never>>, ObjectProper
|
|
|
3732
3728
|
*
|
|
3733
3729
|
* @group Base
|
|
3734
3730
|
*/
|
|
3735
|
-
|
|
3731
|
+
declare const _Object: Type<"Object", Readonly<Record<string, unknown>>, Readonly<Record<string, unknown>>, PlainObjectError, null, PlainObjectError, never, Readonly<Record<string, unknown>>>;
|
|
3732
|
+
export { _Object as Object };
|
|
3736
3733
|
/**
|
|
3737
3734
|
* Record {@link Type}.
|
|
3738
3735
|
*
|
|
@@ -4277,10 +4274,10 @@ export interface ObjectNotObjectError extends TypeError<"Object"> {
|
|
|
4277
4274
|
*
|
|
4278
4275
|
* Object Types accept a `null` prototype or a prototype whose own prototype is
|
|
4279
4276
|
* `null`. This includes ordinary and cross-realm plain objects as well as
|
|
4280
|
-
* objects created from an immediate root prototype. Arrays, built-in
|
|
4281
|
-
* class instances, and objects with deeper custom prototype chains
|
|
4282
|
-
* error instead of having their prototype or inherited state
|
|
4283
|
-
* `reason.value` is the rejected object.
|
|
4277
|
+
* objects created from an immediate root prototype. Arrays, JavaScript built-in
|
|
4278
|
+
* objects, class instances, and objects with deeper custom prototype chains
|
|
4279
|
+
* return this error instead of having their prototype or inherited state
|
|
4280
|
+
* discarded. `reason.value` is the rejected object.
|
|
4284
4281
|
*
|
|
4285
4282
|
* @group Objects
|
|
4286
4283
|
*/
|
|
@@ -4440,8 +4437,8 @@ type OmitKeyConcreteTypeError = CompileTimeError<"Type", "Each omitted key must
|
|
|
4440
4437
|
* Creates a {@link Type} for {@link Result} values.
|
|
4441
4438
|
*
|
|
4442
4439
|
* Use this to validate Results crossing a storage, worker, API, or other
|
|
4443
|
-
* serialization boundary. `fromUnknown` returns an outer validation Result.
|
|
4444
|
-
*
|
|
4440
|
+
* serialization boundary. `fromUnknown` returns an outer validation Result. Its
|
|
4441
|
+
* successful value is the inner domain Result described by `okType` and
|
|
4445
4442
|
* `errorType`.
|
|
4446
4443
|
*
|
|
4447
4444
|
* ### Example
|
|
@@ -4580,7 +4577,8 @@ export interface Typed<Tag extends TypeName> {
|
|
|
4580
4577
|
* Extracts members of a {@link Typed} Output union by their `type` literal.
|
|
4581
4578
|
*
|
|
4582
4579
|
* The requested tag is constrained to the union's actual discriminator values,
|
|
4583
|
-
* so a misspelling is a TypeScript error instead of silently producing
|
|
4580
|
+
* so a misspelling is a TypeScript error instead of silently producing
|
|
4581
|
+
* `never`.
|
|
4584
4582
|
*
|
|
4585
4583
|
* ### Example
|
|
4586
4584
|
*
|
|
@@ -5119,7 +5117,7 @@ export declare const JsonObject: JsonObjectType;
|
|
|
5119
5117
|
* A {@link String} Brand proving that its exact text parses to {@link JsonValue}.
|
|
5120
5118
|
*
|
|
5121
5119
|
* The Brand preserves whitespace, property order, and number spelling. Convert
|
|
5122
|
-
* it
|
|
5120
|
+
* it to {@link JsonValue} through {@link JsonValueFromJson} or
|
|
5123
5121
|
* {@link jsonToJsonValue}.
|
|
5124
5122
|
*
|
|
5125
5123
|
* @group JSON
|
|
@@ -5127,7 +5125,7 @@ export declare const JsonObject: JsonObjectType;
|
|
|
5127
5125
|
export declare const Json: BrandType<Type<"String", string, string, TypeOfError<"String">, null, TypeOfError<"String">, never, string, true>, "Json", JsonError>;
|
|
5128
5126
|
export type Json = typeof Json.Output;
|
|
5129
5127
|
/**
|
|
5130
|
-
*
|
|
5128
|
+
* Converts proven {@link Json} text to an exact {@link JsonValue}.
|
|
5131
5129
|
*
|
|
5132
5130
|
* ### Example
|
|
5133
5131
|
*
|
|
@@ -5143,7 +5141,7 @@ export type Json = typeof Json.Output;
|
|
|
5143
5141
|
*/
|
|
5144
5142
|
export declare const jsonToJsonValue: (value: Json) => JsonValue;
|
|
5145
5143
|
/**
|
|
5146
|
-
*
|
|
5144
|
+
* Converts an exact {@link JsonValue} to canonical {@link Json} text.
|
|
5147
5145
|
*
|
|
5148
5146
|
* ### Example
|
|
5149
5147
|
*
|
|
@@ -5179,67 +5177,48 @@ export declare const jsonValueToJson: (value: JsonValue) => Json;
|
|
|
5179
5177
|
*/
|
|
5180
5178
|
export declare const JsonValueFromJson: TransformType<BrandType<Type<"String", string, string, TypeOfError<"String">, null, TypeOfError<"String">, never, string, true>, "Json", JsonError>, JsonValueType, "JsonValueFromJson", never, string & Brand<"Json">>;
|
|
5181
5179
|
/**
|
|
5182
|
-
* Branded {@link Json} Type and
|
|
5183
|
-
*
|
|
5184
|
-
* Use this
|
|
5185
|
-
*
|
|
5186
|
-
*
|
|
5187
|
-
*
|
|
5188
|
-
* Output.
|
|
5189
|
-
*
|
|
5190
|
-
* The supplied Type's `CanonicalInput` must be JSON-compatible. The encoder
|
|
5191
|
-
* first uses the Type's canonical `to` operation, then encodes that
|
|
5192
|
-
* representation as canonical Json. Runtime representation constraints
|
|
5193
|
-
* TypeScript cannot prove, such as dense Arrays and enumerable data properties,
|
|
5194
|
-
* are asserted as developer errors.
|
|
5195
|
-
*
|
|
5196
|
-
* The branded Json Type is the validation boundary for unknown JSON text. It
|
|
5197
|
-
* grants its {@link Brand} only when the text is valid Json and decoding it
|
|
5198
|
-
* through the supplied Type succeeds. The supplied Type is responsible for
|
|
5199
|
-
* preserving semantic Outputs across canonical JSON encoding and decoding. This
|
|
5200
|
-
* law cannot be checked generically because Types do not define semantic
|
|
5201
|
-
* equality. Before granting the Brand, the encoder asserts the weaker runtime
|
|
5202
|
-
* guarantee that the final Json successfully decodes through the supplied Type.
|
|
5203
|
-
* Failed decodability therefore throws as a developer error.
|
|
5204
|
-
*
|
|
5205
|
-
* Consequently, the two typed conversions return their values directly without
|
|
5206
|
-
* exposing a validation {@link Result}: an Output satisfying the JSON
|
|
5207
|
-
* representation contract of a correctly declared Type can always be encoded,
|
|
5208
|
-
* and the branded Json proves decoding will succeed. Decoding still runs the
|
|
5209
|
-
* Type pipeline because transformations may need to construct different Output
|
|
5210
|
-
* values.
|
|
5180
|
+
* Branded {@link Json} Type and conversions for another {@link Type}.
|
|
5181
|
+
*
|
|
5182
|
+
* Use this when a value must be stored as JSON text, such as in a JSON column
|
|
5183
|
+
* in an Evolu Schema. It returns a branded Json Type and functions for
|
|
5184
|
+
* converting the supplied Type's Output to and from that branded JSON
|
|
5185
|
+
* representation.
|
|
5211
5186
|
*
|
|
5212
5187
|
* ### Example
|
|
5213
5188
|
*
|
|
5214
5189
|
* ```ts
|
|
5215
5190
|
* import {
|
|
5216
5191
|
* Age,
|
|
5192
|
+
* NonEmptyTrimmedString100,
|
|
5217
5193
|
* json,
|
|
5218
5194
|
* object,
|
|
5219
|
-
* String,
|
|
5220
5195
|
* type Brand,
|
|
5221
|
-
* type InferType,
|
|
5222
|
-
* type Json,
|
|
5223
5196
|
* } from "@evolu/common";
|
|
5224
5197
|
*
|
|
5225
|
-
* const
|
|
5226
|
-
*
|
|
5198
|
+
* const User = object({
|
|
5199
|
+
* name: NonEmptyTrimmedString100,
|
|
5200
|
+
* age: Age,
|
|
5201
|
+
* });
|
|
5227
5202
|
*
|
|
5228
|
-
* const [
|
|
5229
|
-
*
|
|
5230
|
-
* "
|
|
5203
|
+
* const [UserJson, userToUserJson, userJsonToUser] = json(
|
|
5204
|
+
* User,
|
|
5205
|
+
* "UserJson",
|
|
5231
5206
|
* );
|
|
5232
|
-
* type PersonJson = typeof PersonJson.Output;
|
|
5233
5207
|
*
|
|
5234
|
-
*
|
|
5235
|
-
*
|
|
5236
|
-
* const person = Person.orThrow({ name: "Ada", age: 42 });
|
|
5237
|
-
* const personJson = personToPersonJson(person);
|
|
5238
|
-
* const decodedPerson = personJsonToPerson(personJson);
|
|
5208
|
+
* const user = User.orThrow({ name: "Ada", age: 37 });
|
|
5209
|
+
* const userJson = userToUserJson(user);
|
|
5239
5210
|
*
|
|
5240
|
-
*
|
|
5211
|
+
* expectTypeOf(userJson).toEqualTypeOf<
|
|
5212
|
+
* string & Brand<"Json"> & Brand<"UserJson">
|
|
5213
|
+
* >();
|
|
5214
|
+
* expect(userJson).toBe('{"name":"Ada","age":37}');
|
|
5215
|
+
* expect(userJsonToUser(userJson)).toEqual(user);
|
|
5241
5216
|
* ```
|
|
5242
5217
|
*
|
|
5218
|
+
* The supplied Type must have a JSON-compatible `CanonicalInput`. The branded
|
|
5219
|
+
* Json Type accepts only valid JSON text whose parsed value can be decoded by
|
|
5220
|
+
* the supplied Type.
|
|
5221
|
+
*
|
|
5243
5222
|
* @group JSON
|
|
5244
5223
|
*/
|
|
5245
5224
|
export declare const json: <T extends ConcreteTypeNode, Name extends TypeName>(type: T, name: ValidateChildTypeName<Name, typeof Json>, ..._validation: [JsonTypeValidationError<T>] extends [never] ? [] : [ValidationFailure<JsonTypeValidationError<T>>]) => readonly [jsonType: BrandType<typeof Json, Name, TypeError<Name> & TransparentTypeError & {
|
|
@@ -5258,5 +5237,4 @@ type JsonNumberCompatible<Value extends number> = [Value] extends [FiniteNumber]
|
|
|
5258
5237
|
type JsonObjectCompatible<Value extends object, Seen extends ReadonlyArray<unknown>> = [keyof Value] extends [never] ? false : string extends keyof Value ? Exclude<keyof Value, string | number> extends never ? Value extends Readonly<Partial<Record<string, infer Item>>> ? IsJsonCompatible<Item, Seen> : false : false : false extends {
|
|
5259
5238
|
readonly [Key in keyof Value]-?: Key extends string ? IsJsonCompatible<{} extends Pick<Value, Key> ? Required<Pick<Value, Key>>[Key] : Value[Key], Seen> : false;
|
|
5260
5239
|
}[keyof Value] ? false : true;
|
|
5261
|
-
export {};
|
|
5262
5240
|
//# sourceMappingURL=Type.d.ts.map
|