@duplojs/utils 1.4.56 → 1.4.57
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/clean/entity.cjs
CHANGED
|
@@ -2,16 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
var kind = require('./kind.cjs');
|
|
4
4
|
var newType = require('./newType.cjs');
|
|
5
|
+
var asserts = require('../common/asserts.cjs');
|
|
5
6
|
var kind$1 = require('../common/kind.cjs');
|
|
6
7
|
var pipe = require('../common/pipe.cjs');
|
|
7
|
-
var filter = require('../array/filter.cjs');
|
|
8
|
-
var isType = require('../common/isType.cjs');
|
|
9
8
|
var map = require('../array/map.cjs');
|
|
10
|
-
var entry = require('../object/entry.cjs');
|
|
11
9
|
var toTuple = require('../array/toTuple.cjs');
|
|
12
10
|
var first = require('../array/at/first.cjs');
|
|
13
11
|
var innerPipe = require('../common/innerPipe.cjs');
|
|
14
12
|
var when = require('../pattern/when.cjs');
|
|
13
|
+
var isType = require('../common/isType.cjs');
|
|
15
14
|
var exhaustive = require('../pattern/exhaustive.cjs');
|
|
16
15
|
var when$1 = require('../common/when.cjs');
|
|
17
16
|
var index = require('../dataParser/parsers/array/index.cjs');
|
|
@@ -24,13 +23,12 @@ var entries = require('../object/entries.cjs');
|
|
|
24
23
|
var forward = require('../common/forward.cjs');
|
|
25
24
|
var errorKindNamespace = require('../common/errorKindNamespace.cjs');
|
|
26
25
|
var fromEntries = require('../object/fromEntries.cjs');
|
|
26
|
+
var entry = require('../object/entry.cjs');
|
|
27
27
|
var transform = require('../dataParser/parsers/transform.cjs');
|
|
28
28
|
var base$1 = require('./constraint/base.cjs');
|
|
29
29
|
var wrapValue = require('../common/wrapValue.cjs');
|
|
30
|
-
var otherwise = require('../pattern/otherwise.cjs');
|
|
31
|
-
var justReturn = require('../common/justReturn.cjs');
|
|
32
|
-
var union = require('../dataParser/parsers/union.cjs');
|
|
33
30
|
var minElements = require('../array/minElements.cjs');
|
|
31
|
+
var union = require('../dataParser/parsers/union.cjs');
|
|
34
32
|
var index$1 = require('../dataParser/parsers/object/index.cjs');
|
|
35
33
|
var override = require('../common/override.cjs');
|
|
36
34
|
var is = require('../either/left/is.cjs');
|
|
@@ -61,7 +59,10 @@ function createEntity(name, getPropertiesDefinition) {
|
|
|
61
59
|
return transform.transform(simplePropertyDefinition.dataParser, (value) => base$1.constrainedTypeKind.setTo(newType.newTypeKind.setTo(wrapValue.wrapValue(value), simplePropertyDefinition.name), constraintKindValue));
|
|
62
60
|
}
|
|
63
61
|
function unionPropertyDefinitionToDataParser(unionPropertyDefinition) {
|
|
64
|
-
return pipe.pipe(unionPropertyDefinition, map.map(simplePropertyDefinitionToDataParser),
|
|
62
|
+
return pipe.pipe(unionPropertyDefinition, map.map(simplePropertyDefinitionToDataParser), (options) => {
|
|
63
|
+
asserts.asserts(options, minElements.minElements(1));
|
|
64
|
+
return union.union(options);
|
|
65
|
+
});
|
|
65
66
|
}
|
|
66
67
|
const params = {
|
|
67
68
|
union: (...type) => ({ type }),
|
|
@@ -99,7 +100,7 @@ function createEntity(name, getPropertiesDefinition) {
|
|
|
99
100
|
}
|
|
100
101
|
return dataParser;
|
|
101
102
|
}, when$1.when(() => definition.nullable === true, nullable.nullable))))), exhaustive.exhaustive),
|
|
102
|
-
])),
|
|
103
|
+
])), fromEntries.fromEntries, index$1.object, (dataParser) => transform.transform(dataParser, (value) => entityKind.setTo(value, name)));
|
|
103
104
|
function map$1(rawProperties) {
|
|
104
105
|
const result = mapDataParser.parse(rawProperties);
|
|
105
106
|
if (is.isLeft(result)) {
|
package/dist/clean/entity.d.ts
CHANGED
|
@@ -27,6 +27,12 @@ export type EntityProperties<GenericPropertiesDefinition extends EntityPropertie
|
|
|
27
27
|
] : readonly InferredValue[] : InferredValue) extends infer InferredValueWithArray ? IsEqual<GenericPropertiesDefinition[Prop]["nullable"], true> extends true ? InferredValueWithArray | null : InferredValueWithArray : never : never : unknown);
|
|
28
28
|
}>;
|
|
29
29
|
export type EntityRawProperties<GenericPropertiesDefinition extends EntityPropertiesDefinition = EntityPropertiesDefinition> = SimplifyTopLevel<{
|
|
30
|
+
readonly [Prop in keyof GenericPropertiesDefinition]: (GenericPropertiesDefinition[Prop] extends EntitySimplePropertyDefinition ? Unwrap<GetNewType<GenericPropertiesDefinition[Prop]>> : GenericPropertiesDefinition[Prop] extends EntityUnionPropertyDefinition ? Unwrap<GetNewType<GenericPropertiesDefinition[Prop][number]>> : GenericPropertiesDefinition[Prop] extends EntityAdvancedPropertyDefinition ? Unwrap<GetNewType<GenericPropertiesDefinition[Prop]["type"] extends EntityUnionPropertyDefinition ? GenericPropertiesDefinition[Prop]["type"][number] : GenericPropertiesDefinition[Prop]["type"] extends EntitySimplePropertyDefinition ? GenericPropertiesDefinition[Prop]["type"] : never>> extends infer InferredValue ? (IsEqual<GenericPropertiesDefinition[Prop]["inArray"], true> extends true ? readonly InferredValue[] : GenericPropertiesDefinition[Prop]["inArray"] extends object ? GenericPropertiesDefinition[Prop]["inArray"]["min"] extends number ? readonly [
|
|
31
|
+
...DArray.CreateTuple<InferredValue, GenericPropertiesDefinition[Prop]["inArray"]["min"]>,
|
|
32
|
+
...InferredValue[]
|
|
33
|
+
] : readonly InferredValue[] : InferredValue) extends infer InferredValueWithArray ? IsEqual<GenericPropertiesDefinition[Prop]["nullable"], true> extends true ? InferredValueWithArray | null : InferredValueWithArray : never : never : unknown);
|
|
34
|
+
}>;
|
|
35
|
+
export type PropertiesToMapOfEntity<GenericPropertiesDefinition extends EntityPropertiesDefinition = EntityPropertiesDefinition> = SimplifyTopLevel<{
|
|
30
36
|
readonly [Prop in keyof GenericPropertiesDefinition]: (GenericPropertiesDefinition[Prop] extends EntitySimplePropertyDefinition ? Unwrap<GetNewType<GenericPropertiesDefinition[Prop]>> : GenericPropertiesDefinition[Prop] extends EntityUnionPropertyDefinition ? Unwrap<GetNewType<GenericPropertiesDefinition[Prop][number]>> : GenericPropertiesDefinition[Prop] extends EntityAdvancedPropertyDefinition ? Unwrap<GetNewType<GenericPropertiesDefinition[Prop]["type"] extends EntityUnionPropertyDefinition ? GenericPropertiesDefinition[Prop]["type"][number] : GenericPropertiesDefinition[Prop]["type"] extends EntitySimplePropertyDefinition ? GenericPropertiesDefinition[Prop]["type"] : never>> extends infer InferredValue ? (Or<[
|
|
31
37
|
IsEqual<GenericPropertiesDefinition[Prop]["inArray"], true>,
|
|
32
38
|
IsExtends<GenericPropertiesDefinition[Prop]["inArray"], object>
|
|
@@ -47,11 +53,7 @@ export interface EntityHandler<GenericName extends string = string, GenericPrope
|
|
|
47
53
|
*
|
|
48
54
|
*/
|
|
49
55
|
readonly propertiesDefinition: GenericPropertiesDefinition;
|
|
50
|
-
|
|
51
|
-
* The DataParser derived from the properties definition. Useful to reuse validation outside entity creation.
|
|
52
|
-
*
|
|
53
|
-
*/
|
|
54
|
-
readonly mapDataParser: DDataParser.Contract<EntityProperties<GenericPropertiesDefinition>, EntityRawProperties<GenericPropertiesDefinition>>;
|
|
56
|
+
readonly mapDataParser: DDataParser.Contract<EntityProperties<GenericPropertiesDefinition>, unknown>;
|
|
55
57
|
/**
|
|
56
58
|
* Builds an entity from already typed properties.
|
|
57
59
|
*
|
|
@@ -70,7 +72,7 @@ export interface EntityHandler<GenericName extends string = string, GenericPrope
|
|
|
70
72
|
*/
|
|
71
73
|
"new"<const GenericProperties extends EntityProperties<GenericPropertiesDefinition>>(properties: GenericProperties): Entity<GenericName> & GenericProperties;
|
|
72
74
|
/**
|
|
73
|
-
* Validates raw properties and returns an Either with the typed entity.
|
|
75
|
+
* Validates permissive raw properties and returns an Either with the typed entity.
|
|
74
76
|
*
|
|
75
77
|
* ```ts
|
|
76
78
|
* if (User.Entity.is(result)) {
|
|
@@ -85,9 +87,9 @@ export interface EntityHandler<GenericName extends string = string, GenericPrope
|
|
|
85
87
|
* ```
|
|
86
88
|
*
|
|
87
89
|
*/
|
|
88
|
-
map(rawProperties:
|
|
90
|
+
map(rawProperties: PropertiesToMapOfEntity<GenericPropertiesDefinition>): (DEither.Right<"createEntity", Entity<GenericName> & EntityProperties<GenericPropertiesDefinition>> | DEither.Left<"createEntityError", DDataParser.DataParserError>);
|
|
89
91
|
/**
|
|
90
|
-
* Validates raw properties and throws on error.
|
|
92
|
+
* Validates permissive raw properties and throws on error.
|
|
91
93
|
*
|
|
92
94
|
* ```ts
|
|
93
95
|
* const mapped = User.Entity.mapOrThrow({
|
|
@@ -99,7 +101,7 @@ export interface EntityHandler<GenericName extends string = string, GenericPrope
|
|
|
99
101
|
* ```
|
|
100
102
|
*
|
|
101
103
|
*/
|
|
102
|
-
mapOrThrow(rawProperties:
|
|
104
|
+
mapOrThrow(rawProperties: PropertiesToMapOfEntity<GenericPropertiesDefinition>): Entity<GenericName> & EntityProperties<GenericPropertiesDefinition>;
|
|
103
105
|
/**
|
|
104
106
|
* Checks if a value is an entity of this handler (type guard).
|
|
105
107
|
*
|
|
@@ -129,9 +131,9 @@ declare const CreateEntityError_base: new (params: {
|
|
|
129
131
|
"@DuplojsUtilsError/create-entity-error"?: unknown;
|
|
130
132
|
}, parentParams: readonly [message?: string | undefined, options?: ErrorOptions | undefined]) => Error & Kind<import("../common").KindDefinition<"create-entity-error", unknown>, unknown> & Kind<import("../common").KindDefinition<"@DuplojsUtilsError/create-entity-error", unknown>, unknown>;
|
|
131
133
|
export declare class CreateEntityError extends CreateEntityError_base {
|
|
132
|
-
rawProperties:
|
|
134
|
+
rawProperties: PropertiesToMapOfEntity;
|
|
133
135
|
dataParserError: DDataParser.DataParserError;
|
|
134
|
-
constructor(rawProperties:
|
|
136
|
+
constructor(rawProperties: PropertiesToMapOfEntity, dataParserError: DDataParser.DataParserError);
|
|
135
137
|
}
|
|
136
138
|
export interface PropertiesDefinitionParams {
|
|
137
139
|
union<const GenericUnionPropertyDefinition extends EntityUnionPropertyDefinition>(...type: GenericUnionPropertyDefinition): {
|
|
@@ -216,7 +218,7 @@ export interface PropertiesDefinitionParams {
|
|
|
216
218
|
*
|
|
217
219
|
* @remarks
|
|
218
220
|
* - The definition callback can use helpers like `array`, `nullable`, and `union` to enrich properties.
|
|
219
|
-
* - Use `map`/`mapOrThrow` to build from raw inputs; `new` expects already typed values.
|
|
221
|
+
* - Use `map`/`mapOrThrow` to build from raw inputs (with runtime constraint checks); `new` expects already typed values.
|
|
220
222
|
*
|
|
221
223
|
* @see https://utils.duplojs.dev/en/v1/api/clean/entity
|
|
222
224
|
*
|
package/dist/clean/entity.mjs
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { createCleanKind } from './kind.mjs';
|
|
2
2
|
import { newTypeHandlerKind, newTypeKind } from './newType.mjs';
|
|
3
|
+
import { asserts } from '../common/asserts.mjs';
|
|
3
4
|
import { kindHeritage } from '../common/kind.mjs';
|
|
4
5
|
import { pipe } from '../common/pipe.mjs';
|
|
5
|
-
import { filter } from '../array/filter.mjs';
|
|
6
|
-
import { isType } from '../common/isType.mjs';
|
|
7
6
|
import { map } from '../array/map.mjs';
|
|
8
|
-
import { entry } from '../object/entry.mjs';
|
|
9
7
|
import { toTuple } from '../array/toTuple.mjs';
|
|
10
8
|
import { first } from '../array/at/first.mjs';
|
|
11
9
|
import { innerPipe } from '../common/innerPipe.mjs';
|
|
12
10
|
import { when } from '../pattern/when.mjs';
|
|
11
|
+
import { isType } from '../common/isType.mjs';
|
|
13
12
|
import { exhaustive } from '../pattern/exhaustive.mjs';
|
|
14
13
|
import { when as when$1 } from '../common/when.mjs';
|
|
15
14
|
import { array } from '../dataParser/parsers/array/index.mjs';
|
|
@@ -22,13 +21,12 @@ import { entries } from '../object/entries.mjs';
|
|
|
22
21
|
import { forward } from '../common/forward.mjs';
|
|
23
22
|
import { createErrorKind } from '../common/errorKindNamespace.mjs';
|
|
24
23
|
import { fromEntries } from '../object/fromEntries.mjs';
|
|
24
|
+
import { entry } from '../object/entry.mjs';
|
|
25
25
|
import { transform } from '../dataParser/parsers/transform.mjs';
|
|
26
26
|
import { constrainedTypeKind } from './constraint/base.mjs';
|
|
27
27
|
import { wrapValue } from '../common/wrapValue.mjs';
|
|
28
|
-
import { otherwise } from '../pattern/otherwise.mjs';
|
|
29
|
-
import { justReturn } from '../common/justReturn.mjs';
|
|
30
|
-
import { union } from '../dataParser/parsers/union.mjs';
|
|
31
28
|
import { minElements } from '../array/minElements.mjs';
|
|
29
|
+
import { union } from '../dataParser/parsers/union.mjs';
|
|
32
30
|
import { object } from '../dataParser/parsers/object/index.mjs';
|
|
33
31
|
import { createOverride } from '../common/override.mjs';
|
|
34
32
|
import { isLeft } from '../either/left/is.mjs';
|
|
@@ -59,7 +57,10 @@ function createEntity(name, getPropertiesDefinition) {
|
|
|
59
57
|
return transform(simplePropertyDefinition.dataParser, (value) => constrainedTypeKind.setTo(newTypeKind.setTo(wrapValue(value), simplePropertyDefinition.name), constraintKindValue));
|
|
60
58
|
}
|
|
61
59
|
function unionPropertyDefinitionToDataParser(unionPropertyDefinition) {
|
|
62
|
-
return pipe(unionPropertyDefinition, map(simplePropertyDefinitionToDataParser),
|
|
60
|
+
return pipe(unionPropertyDefinition, map(simplePropertyDefinitionToDataParser), (options) => {
|
|
61
|
+
asserts(options, minElements(1));
|
|
62
|
+
return union(options);
|
|
63
|
+
});
|
|
63
64
|
}
|
|
64
65
|
const params = {
|
|
65
66
|
union: (...type) => ({ type }),
|
|
@@ -97,7 +98,7 @@ function createEntity(name, getPropertiesDefinition) {
|
|
|
97
98
|
}
|
|
98
99
|
return dataParser;
|
|
99
100
|
}, when$1(() => definition.nullable === true, nullable))))), exhaustive),
|
|
100
|
-
])),
|
|
101
|
+
])), fromEntries, object, (dataParser) => transform(dataParser, (value) => entityKind.setTo(value, name)));
|
|
101
102
|
function map$1(rawProperties) {
|
|
102
103
|
const result = mapDataParser.parse(rawProperties);
|
|
103
104
|
if (isLeft(result)) {
|
|
@@ -256,5 +256,5 @@ export declare namespace dataParserInit {
|
|
|
256
256
|
}
|
|
257
257
|
export type Output<GenericDataParser extends DataParser> = GetKindValue<typeof dataParserKind, GenericDataParser>["output"];
|
|
258
258
|
export type Input<GenericDataParser extends DataParser> = GetKindValue<typeof dataParserKind, GenericDataParser>["input"];
|
|
259
|
-
export type Contract<GenericOutput extends unknown, GenericInput extends unknown = GenericOutput> = DataParser<DataParserDefinition
|
|
259
|
+
export type Contract<GenericOutput extends unknown, GenericInput extends unknown = GenericOutput> = DataParser<DataParserDefinition, GenericOutput, GenericInput>;
|
|
260
260
|
export {};
|
|
@@ -415,5 +415,5 @@ export declare function dataParserExtendedInit<GenericDataParser extends DataPar
|
|
|
415
415
|
export declare namespace dataParserExtendedInit {
|
|
416
416
|
var overrideHandler: OverrideHandler<DataParserExtended<DataParserDefinition<import("./base").DataParserChecker<import("./base").DataParserCheckerDefinition, unknown>>, unknown, unknown>>;
|
|
417
417
|
}
|
|
418
|
-
export type ContractExtended<GenericOutput extends unknown, GenericInput extends unknown = GenericOutput> = DataParserExtended<DataParserDefinition
|
|
418
|
+
export type ContractExtended<GenericOutput extends unknown, GenericInput extends unknown = GenericOutput> = DataParserExtended<DataParserDefinition, GenericOutput, GenericInput>;
|
|
419
419
|
export {};
|