@strictly/define 0.0.11 → 0.0.13
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/.out/transformers/flatteners/json_path.d.ts +6 -2
- package/.out/transformers/flatteners/json_path.js +15 -2
- package/.out/transformers/flatteners/specs/json_paths.tests.d.ts +1 -0
- package/.out/transformers/flatteners/specs/json_paths.tests.js +50 -0
- package/.out/tsconfig.tsbuildinfo +1 -1
- package/.turbo/turbo-build.log +8 -8
- package/.turbo/turbo-check-types.log +1 -1
- package/.turbo/turbo-release$colon$exports.log +1 -1
- package/dist/index.cjs +41 -28
- package/dist/index.d.cts +7 -4
- package/dist/index.d.ts +7 -4
- package/dist/index.js +18 -4
- package/package.json +1 -1
- package/transformers/flatteners/json_path.ts +44 -4
- package/transformers/flatteners/specs/json_paths.tests.ts +69 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -7,12 +7,12 @@ $ tsup
|
|
|
7
7
|
[34mCLI[39m Target: es6
|
|
8
8
|
[34mCJS[39m Build start
|
|
9
9
|
[34mESM[39m Build start
|
|
10
|
-
[32mCJS[39m [1mdist/index.cjs [22m[
|
|
11
|
-
[32mCJS[39m ⚡️ Build success in
|
|
12
|
-
[32mESM[39m [1mdist/index.js [22m[32m28.
|
|
13
|
-
[32mESM[39m ⚡️ Build success in
|
|
10
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m31.10 KB[39m
|
|
11
|
+
[32mCJS[39m ⚡️ Build success in 55ms
|
|
12
|
+
[32mESM[39m [1mdist/index.js [22m[32m28.71 KB[39m
|
|
13
|
+
[32mESM[39m ⚡️ Build success in 56ms
|
|
14
14
|
[34mDTS[39m Build start
|
|
15
|
-
[32mDTS[39m ⚡️ Build success in
|
|
16
|
-
[32mDTS[39m [1mdist/index.d.cts [22m[32m40.
|
|
17
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[32m40.
|
|
18
|
-
Done in 6.
|
|
15
|
+
[32mDTS[39m ⚡️ Build success in 5419ms
|
|
16
|
+
[32mDTS[39m [1mdist/index.d.cts [22m[32m40.77 KB[39m
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m40.77 KB[39m
|
|
18
|
+
Done in 6.25s.
|
package/dist/index.cjs
CHANGED
|
@@ -59,6 +59,8 @@ __export(index_exports, {
|
|
|
59
59
|
isFunctionalValidator: () => isFunctionalValidator,
|
|
60
60
|
jsonPath: () => jsonPath,
|
|
61
61
|
jsonPathPop: () => jsonPathPop,
|
|
62
|
+
jsonPathPrefix: () => jsonPathPrefix,
|
|
63
|
+
jsonPathUnprefix: () => jsonPathUnprefix,
|
|
62
64
|
list: () => list,
|
|
63
65
|
literal: () => literal,
|
|
64
66
|
mergeAnnotations: () => mergeAnnotations,
|
|
@@ -220,15 +222,16 @@ function copy(t, proto) {
|
|
|
220
222
|
}
|
|
221
223
|
|
|
222
224
|
// transformers/copies/mobx_copy.ts
|
|
223
|
-
var
|
|
225
|
+
var import_base4 = require("@strictly/base");
|
|
224
226
|
var import_mobx = require("mobx");
|
|
225
227
|
|
|
226
228
|
// transformers/flatteners/flatten_value_to.ts
|
|
227
|
-
var
|
|
229
|
+
var import_base3 = require("@strictly/base");
|
|
228
230
|
|
|
229
231
|
// transformers/flatteners/json_path.ts
|
|
230
|
-
|
|
231
|
-
|
|
232
|
+
var import_base2 = require("@strictly/base");
|
|
233
|
+
function jsonPath(prefix, segment, qualifier) {
|
|
234
|
+
const s = `.${qualifier != null ? qualifier : ""}${segment}`;
|
|
232
235
|
return `${prefix}${s}`;
|
|
233
236
|
}
|
|
234
237
|
function jsonPathPop(path) {
|
|
@@ -241,6 +244,14 @@ function jsonPathPop(path) {
|
|
|
241
244
|
parts.pop()
|
|
242
245
|
];
|
|
243
246
|
}
|
|
247
|
+
function jsonPathPrefix(prefix, path) {
|
|
248
|
+
(0, import_base2.assertEqual)(path[0], "$", "{} should start with $", path);
|
|
249
|
+
return `${prefix}${path.slice(1)}`;
|
|
250
|
+
}
|
|
251
|
+
function jsonPathUnprefix(prefix, path) {
|
|
252
|
+
(0, import_base2.assertState)(path.startsWith(prefix), "{} should start with {}", path, prefix);
|
|
253
|
+
return `$${path.slice(prefix.length)}`;
|
|
254
|
+
}
|
|
244
255
|
|
|
245
256
|
// transformers/flatteners/flatten_value_to.ts
|
|
246
257
|
function flattenValueTo({ definition }, v, setter, mapper2) {
|
|
@@ -276,7 +287,7 @@ function internalFlattenValueChildren(valuePath, typePath, qualifier, typeDef, v
|
|
|
276
287
|
case 5 /* Union */:
|
|
277
288
|
return internalFlattenUnionChildren(valuePath, typePath, qualifier, typeDef, v, mapper2, r);
|
|
278
289
|
default:
|
|
279
|
-
throw new
|
|
290
|
+
throw new import_base3.UnreachableError(typeDef);
|
|
280
291
|
}
|
|
281
292
|
}
|
|
282
293
|
function internalFlattenListChildren(valuePath, typePath, { elements }, v, mapper2, r) {
|
|
@@ -297,7 +308,7 @@ function internalFlattenListChildren(valuePath, typePath, { elements }, v, mappe
|
|
|
297
308
|
}
|
|
298
309
|
function internalFlattenRecordChildren(valuePath, typePath, { valueTypeDef }, v, mapper2, r) {
|
|
299
310
|
const newTypePath = jsonPath(typePath, "*");
|
|
300
|
-
return (0,
|
|
311
|
+
return (0, import_base3.reduce)(
|
|
301
312
|
v,
|
|
302
313
|
function(r2, k, value) {
|
|
303
314
|
return internalFlattenValue(
|
|
@@ -316,7 +327,7 @@ function internalFlattenRecordChildren(valuePath, typePath, { valueTypeDef }, v,
|
|
|
316
327
|
);
|
|
317
328
|
}
|
|
318
329
|
function internalFlattenObjectChildren(valuePath, typePath, qualifier, { fields }, v, mapper2, r) {
|
|
319
|
-
return (0,
|
|
330
|
+
return (0, import_base3.reduce)(
|
|
320
331
|
fields,
|
|
321
332
|
function(r2, k, fieldTypeDef) {
|
|
322
333
|
const fieldValue = v[k];
|
|
@@ -350,7 +361,7 @@ function internalFlattenUnionChildren(valuePath, typePath, qualifier, typeDef, v
|
|
|
350
361
|
}
|
|
351
362
|
function getUnionTypeDef(typeDef, v) {
|
|
352
363
|
if (typeDef.discriminator == null) {
|
|
353
|
-
return (0,
|
|
364
|
+
return (0, import_base3.reduce)(
|
|
354
365
|
typeDef.unions,
|
|
355
366
|
function(acc, _k, t) {
|
|
356
367
|
if (t.type === 1 /* Literal */ && t.valuePrototype) {
|
|
@@ -391,7 +402,7 @@ function observeValue(v, def) {
|
|
|
391
402
|
case 4 /* Object */:
|
|
392
403
|
return (0, import_mobx.makeObservable)(
|
|
393
404
|
v,
|
|
394
|
-
(0,
|
|
405
|
+
(0, import_base4.reduce)(
|
|
395
406
|
def.fields,
|
|
396
407
|
function(acc, k) {
|
|
397
408
|
acc[k] = import_mobx.observable;
|
|
@@ -407,7 +418,7 @@ function observeValue(v, def) {
|
|
|
407
418
|
case 5 /* Union */:
|
|
408
419
|
return observeValue(v, getUnionTypeDef(def, v));
|
|
409
420
|
default:
|
|
410
|
-
throw new
|
|
421
|
+
throw new import_base4.UnreachableError(def);
|
|
411
422
|
}
|
|
412
423
|
}
|
|
413
424
|
function mobxCopy(t, proto) {
|
|
@@ -445,10 +456,10 @@ function flattenJsonValueToTypePathsOf(t, value) {
|
|
|
445
456
|
}
|
|
446
457
|
|
|
447
458
|
// transformers/flatteners/flatten_type_to.ts
|
|
448
|
-
var
|
|
459
|
+
var import_base5 = require("@strictly/base");
|
|
449
460
|
function flattenTypeTo({ definition }, mapper2) {
|
|
450
461
|
const typeDefs = internalFlattenTypeDef("$", definition, {});
|
|
451
|
-
return (0,
|
|
462
|
+
return (0, import_base5.reduce)(
|
|
452
463
|
typeDefs,
|
|
453
464
|
function(acc, key, typeDef) {
|
|
454
465
|
acc[key] = mapper2(typeDef, key);
|
|
@@ -475,7 +486,7 @@ function internalFlattenTypeDefChildren(path, qualifier, t, r) {
|
|
|
475
486
|
case 5 /* Union */:
|
|
476
487
|
return internalFlattenUnionTypeDefChildren(path, qualifier, t, r);
|
|
477
488
|
default:
|
|
478
|
-
throw new
|
|
489
|
+
throw new import_base5.UnreachableError(t);
|
|
479
490
|
}
|
|
480
491
|
}
|
|
481
492
|
function internalFlattenedListTypeDefChildren(path, { elements }, r) {
|
|
@@ -485,7 +496,7 @@ function internalFlattenRecordTypeDefChildren(path, { valueTypeDef }, r) {
|
|
|
485
496
|
return internalFlattenTypeDef(jsonPath(path, "*"), valueTypeDef, r);
|
|
486
497
|
}
|
|
487
498
|
function internalFlattenObjectTypeDefChildren(path, qualifier, { fields }, r) {
|
|
488
|
-
return (0,
|
|
499
|
+
return (0, import_base5.reduce)(
|
|
489
500
|
fields,
|
|
490
501
|
function(acc, fieldName, fieldTypeDef) {
|
|
491
502
|
return internalFlattenTypeDef(
|
|
@@ -501,7 +512,7 @@ function internalFlattenUnionTypeDefChildren(path, qualifier, {
|
|
|
501
512
|
discriminator,
|
|
502
513
|
unions
|
|
503
514
|
}, r) {
|
|
504
|
-
return (0,
|
|
515
|
+
return (0, import_base5.reduce)(
|
|
505
516
|
unions,
|
|
506
517
|
function(acc, key, typeDef) {
|
|
507
518
|
return internalFlattenTypeDefChildren(
|
|
@@ -561,10 +572,10 @@ function flattenValuesOfType(typeDef, value) {
|
|
|
561
572
|
}
|
|
562
573
|
|
|
563
574
|
// transformers/flatteners/value_path_to_type_path.ts
|
|
564
|
-
var
|
|
575
|
+
var import_base6 = require("@strictly/base");
|
|
565
576
|
function valuePathToTypePath({ definition: typeDef }, valuePath, allowMissingPaths = false) {
|
|
566
577
|
const valueSteps = valuePath.split(/\.|\[/g);
|
|
567
|
-
(0,
|
|
578
|
+
(0, import_base6.assertEqual)(valueSteps[0], "$");
|
|
568
579
|
const typeSteps = internalJsonValuePathToTypePath(
|
|
569
580
|
typeDef,
|
|
570
581
|
valueSteps.slice(1),
|
|
@@ -588,7 +599,7 @@ function internalJsonValuePathToTypePath(typeDef, valueSteps, allowMissingPaths,
|
|
|
588
599
|
if (allowMissingPaths) {
|
|
589
600
|
return valueSteps;
|
|
590
601
|
} else {
|
|
591
|
-
throw new
|
|
602
|
+
throw new import_base6.PreconditionFailedError(
|
|
592
603
|
"literal should terminate path {} ({})",
|
|
593
604
|
originalValuePath,
|
|
594
605
|
valueStep
|
|
@@ -620,7 +631,7 @@ function internalJsonValuePathToTypePath(typeDef, valueSteps, allowMissingPaths,
|
|
|
620
631
|
return valueSteps;
|
|
621
632
|
}
|
|
622
633
|
} else {
|
|
623
|
-
(0,
|
|
634
|
+
(0, import_base6.assertExists)(typeDef.fields[valueStep], "missing field in {} ({})", originalValuePath, valueStep);
|
|
624
635
|
}
|
|
625
636
|
return [
|
|
626
637
|
valueStep,
|
|
@@ -634,7 +645,7 @@ function internalJsonValuePathToTypePath(typeDef, valueSteps, allowMissingPaths,
|
|
|
634
645
|
case 5 /* Union */:
|
|
635
646
|
if (typeDef.discriminator == null) {
|
|
636
647
|
if (remainingValueSteps.length > 0) {
|
|
637
|
-
const union2 = (0,
|
|
648
|
+
const union2 = (0, import_base6.reduce)(
|
|
638
649
|
typeDef.unions,
|
|
639
650
|
function(acc, _k, v) {
|
|
640
651
|
if (v.type !== 1 /* Literal */ || v.type === 1 /* Literal */ && v.valuePrototype == null) {
|
|
@@ -644,7 +655,7 @@ function internalJsonValuePathToTypePath(typeDef, valueSteps, allowMissingPaths,
|
|
|
644
655
|
},
|
|
645
656
|
null
|
|
646
657
|
);
|
|
647
|
-
(0,
|
|
658
|
+
(0, import_base6.assertExists)(union2, "expected a complex union {}", originalValuePath);
|
|
648
659
|
return internalJsonValuePathToTypePath(
|
|
649
660
|
union2,
|
|
650
661
|
valueSteps,
|
|
@@ -660,7 +671,7 @@ function internalJsonValuePathToTypePath(typeDef, valueSteps, allowMissingPaths,
|
|
|
660
671
|
if (allowMissingPaths) {
|
|
661
672
|
return valueSteps;
|
|
662
673
|
} else {
|
|
663
|
-
throw new
|
|
674
|
+
throw new import_base6.PreconditionFailedError(
|
|
664
675
|
"mismatched qualifiers in {} (at {})",
|
|
665
676
|
originalValuePath,
|
|
666
677
|
valueStep
|
|
@@ -669,7 +680,7 @@ function internalJsonValuePathToTypePath(typeDef, valueSteps, allowMissingPaths,
|
|
|
669
680
|
}
|
|
670
681
|
const qualifier = valueStep.substring(0, qualifierIndex);
|
|
671
682
|
const remainder = valueStep.substring(qualifierIndex + 1);
|
|
672
|
-
const union2 = (0,
|
|
683
|
+
const union2 = (0, import_base6.assertExistsAndReturn)(typeDef.unions[qualifier], "missing union {}", qualifier);
|
|
673
684
|
const [
|
|
674
685
|
typeStep,
|
|
675
686
|
...remainingTypeSteps
|
|
@@ -688,7 +699,7 @@ function internalJsonValuePathToTypePath(typeDef, valueSteps, allowMissingPaths,
|
|
|
688
699
|
];
|
|
689
700
|
}
|
|
690
701
|
default:
|
|
691
|
-
throw new
|
|
702
|
+
throw new import_base6.UnreachableError(typeDef);
|
|
692
703
|
}
|
|
693
704
|
}
|
|
694
705
|
|
|
@@ -724,7 +735,7 @@ function mergeAnnotations(a1, a2) {
|
|
|
724
735
|
}
|
|
725
736
|
|
|
726
737
|
// types/type_of_type.ts
|
|
727
|
-
var
|
|
738
|
+
var import_base7 = require("@strictly/base");
|
|
728
739
|
function typeOfType({ definition }) {
|
|
729
740
|
return {
|
|
730
741
|
definition: typeDefOfTypeDef(definition)
|
|
@@ -743,7 +754,7 @@ function typeDefOfTypeDef(t) {
|
|
|
743
754
|
case 5 /* Union */:
|
|
744
755
|
return typeDefOfUnionTypeDef(t);
|
|
745
756
|
default:
|
|
746
|
-
throw new
|
|
757
|
+
throw new import_base7.UnreachableError(t);
|
|
747
758
|
}
|
|
748
759
|
}
|
|
749
760
|
function typeDefOfLiteralTypeDef({
|
|
@@ -781,7 +792,7 @@ function typeDefOfObjectTypeDef({
|
|
|
781
792
|
}) {
|
|
782
793
|
return {
|
|
783
794
|
type,
|
|
784
|
-
fields: (0,
|
|
795
|
+
fields: (0, import_base7.map)(fields, function(_k, v) {
|
|
785
796
|
return typeDefOfTypeDef(v);
|
|
786
797
|
})
|
|
787
798
|
};
|
|
@@ -794,7 +805,7 @@ function typeDefOfUnionTypeDef({
|
|
|
794
805
|
return {
|
|
795
806
|
type,
|
|
796
807
|
discriminator,
|
|
797
|
-
unions: (0,
|
|
808
|
+
unions: (0, import_base7.map)(unions, function(_k, v) {
|
|
798
809
|
return typeDefOfTypeDef(v);
|
|
799
810
|
})
|
|
800
811
|
};
|
|
@@ -1137,6 +1148,8 @@ var RegexpValidator = _RegexpValidator;
|
|
|
1137
1148
|
isFunctionalValidator,
|
|
1138
1149
|
jsonPath,
|
|
1139
1150
|
jsonPathPop,
|
|
1151
|
+
jsonPathPrefix,
|
|
1152
|
+
jsonPathUnprefix,
|
|
1140
1153
|
list,
|
|
1141
1154
|
literal,
|
|
1142
1155
|
mergeAnnotations,
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { IsEqual, Simplify, UnionToIntersection, SimplifyDeep } from 'type-fest';
|
|
2
|
-
import { IsFieldReadonly } from '@strictly/base';
|
|
2
|
+
import { IsFieldReadonly, StringConcatOf } from '@strictly/base';
|
|
3
3
|
|
|
4
4
|
type Type<T extends TypeDef = TypeDef> = {
|
|
5
5
|
readonly definition: T;
|
|
@@ -298,8 +298,11 @@ declare function flattenValidatorsOfValidatingType<T extends ValidatingType, Typ
|
|
|
298
298
|
|
|
299
299
|
declare function flattenValuesOfType<T extends Type>(typeDef: Type, value: ValueOfType<T>): Readonly<Record<string, any>>;
|
|
300
300
|
|
|
301
|
-
declare function jsonPath
|
|
302
|
-
declare function
|
|
301
|
+
declare function jsonPath<Prefix extends string, Segment extends number | string>(prefix: Prefix, segment: Segment): `${Prefix}.${Segment}`;
|
|
302
|
+
declare function jsonPath<Prefix extends string, Segment extends number | string, Qualifier extends string>(prefix: Prefix, segment: Segment, qualifier: Qualifier): `${Prefix}.${Qualifier}${Segment}`;
|
|
303
|
+
declare function jsonPathPop<Path extends string>(path: Path): [string, string] | null;
|
|
304
|
+
declare function jsonPathPrefix<Prefix extends string, Path extends string>(prefix: Prefix, path: Path): Path extends StringConcatOf<'$', infer ToMount> ? `${Prefix}${ToMount}` : never;
|
|
305
|
+
declare function jsonPathUnprefix<Prefix extends string, Path extends string>(prefix: Prefix, path: Path): Path extends StringConcatOf<Prefix, infer ToUnmount> ? `$${ToUnmount}` : never;
|
|
303
306
|
|
|
304
307
|
declare function valuePathToTypePath<ValuePathsToTypePaths extends Record<string, string>, ValuePath extends keyof ValuePathsToTypePaths>({ definition: typeDef }: Type, valuePath: ValuePath, allowMissingPaths?: boolean): ValuePathsToTypePaths[ValuePath];
|
|
305
308
|
|
|
@@ -592,4 +595,4 @@ type ValidatorsOfValues<FlattenedValues extends Readonly<Record<string, any>>, T
|
|
|
592
595
|
readonly [K in keyof FlattenedValues]: Validator<FlattenedValues[K], any, TypePathsToValuePaths[K], Context>;
|
|
593
596
|
};
|
|
594
597
|
|
|
595
|
-
export { type Accessor, type AnnotatedValidator, type Annotations, type AnyValueType, DefinedValidator, type ErrorOfValidator, type FlattenedAccessorsOfType, type FlattenedTypesOfType, type FlattenedValuesOfType, type FunctionalValidator, type InternalJsonPathsOf, type IsStrictUnion, type ListTypeDef, type LiteralTypeDef, type Mapper, type MinimumStringLengthValidationError, MinimumStringLengthValidationErrorType, MinimumStringLengthValidator, type MobxObservable, type MobxValueOfType, type NonMobxObservable, type ObjectFieldKey, type ObjectTypeDef, type ObjectTypeDefFields, OptionalValidatorProxy, type PathsOfType, type ReadonlyOfTypeDef, type ReadonlyTypeOfType, type RecordKeyType, type RecordTypeDef, type RegexpValidationError, RegexpValidationErrorType, RegexpValidator, type Setter, type StrictListTypeDef, type StrictLiteralTypeDef, type StrictObjectTypeDef, type StrictObjectTypeDefFields, type StrictRecordTypeDef, type StrictType, type StrictTypeDef, type StrictUnionTypeDef, type Type, type TypeDef, TypeDefType, type UnionKey, type UnionTypeDef, type ValidationError, type Validator, type ValidatorsOfValues, type ValueOfType, type ValueOfTypeDef, type ValueToTypePathsOfType, type ValueTypesOfDiscriminatedUnion, annotations, booleanType, copy, flattenAccessorsOfType, flattenJsonValueToTypePathsOf, flattenTypesOfType, flattenValidatorsOfValidatingType, flattenValueTo, flattenValuesOfType, getUnionTypeDef, isAnnotatedValidator, isFunctionalValidator, jsonPath, jsonPathPop, list, literal, mergeAnnotations, mobxCopy, nullType, nullable, numberType, object, record, stringType, union, validate, valuePathToTypePath };
|
|
598
|
+
export { type Accessor, type AnnotatedValidator, type Annotations, type AnyValueType, DefinedValidator, type ErrorOfValidator, type FlattenedAccessorsOfType, type FlattenedTypesOfType, type FlattenedValuesOfType, type FunctionalValidator, type InternalJsonPathsOf, type IsStrictUnion, type ListTypeDef, type LiteralTypeDef, type Mapper, type MinimumStringLengthValidationError, MinimumStringLengthValidationErrorType, MinimumStringLengthValidator, type MobxObservable, type MobxValueOfType, type NonMobxObservable, type ObjectFieldKey, type ObjectTypeDef, type ObjectTypeDefFields, OptionalValidatorProxy, type PathsOfType, type ReadonlyOfTypeDef, type ReadonlyTypeOfType, type RecordKeyType, type RecordTypeDef, type RegexpValidationError, RegexpValidationErrorType, RegexpValidator, type Setter, type StrictListTypeDef, type StrictLiteralTypeDef, type StrictObjectTypeDef, type StrictObjectTypeDefFields, type StrictRecordTypeDef, type StrictType, type StrictTypeDef, type StrictUnionTypeDef, type Type, type TypeDef, TypeDefType, type UnionKey, type UnionTypeDef, type ValidationError, type Validator, type ValidatorsOfValues, type ValueOfType, type ValueOfTypeDef, type ValueToTypePathsOfType, type ValueTypesOfDiscriminatedUnion, annotations, booleanType, copy, flattenAccessorsOfType, flattenJsonValueToTypePathsOf, flattenTypesOfType, flattenValidatorsOfValidatingType, flattenValueTo, flattenValuesOfType, getUnionTypeDef, isAnnotatedValidator, isFunctionalValidator, jsonPath, jsonPathPop, jsonPathPrefix, jsonPathUnprefix, list, literal, mergeAnnotations, mobxCopy, nullType, nullable, numberType, object, record, stringType, union, validate, valuePathToTypePath };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { IsEqual, Simplify, UnionToIntersection, SimplifyDeep } from 'type-fest';
|
|
2
|
-
import { IsFieldReadonly } from '@strictly/base';
|
|
2
|
+
import { IsFieldReadonly, StringConcatOf } from '@strictly/base';
|
|
3
3
|
|
|
4
4
|
type Type<T extends TypeDef = TypeDef> = {
|
|
5
5
|
readonly definition: T;
|
|
@@ -298,8 +298,11 @@ declare function flattenValidatorsOfValidatingType<T extends ValidatingType, Typ
|
|
|
298
298
|
|
|
299
299
|
declare function flattenValuesOfType<T extends Type>(typeDef: Type, value: ValueOfType<T>): Readonly<Record<string, any>>;
|
|
300
300
|
|
|
301
|
-
declare function jsonPath
|
|
302
|
-
declare function
|
|
301
|
+
declare function jsonPath<Prefix extends string, Segment extends number | string>(prefix: Prefix, segment: Segment): `${Prefix}.${Segment}`;
|
|
302
|
+
declare function jsonPath<Prefix extends string, Segment extends number | string, Qualifier extends string>(prefix: Prefix, segment: Segment, qualifier: Qualifier): `${Prefix}.${Qualifier}${Segment}`;
|
|
303
|
+
declare function jsonPathPop<Path extends string>(path: Path): [string, string] | null;
|
|
304
|
+
declare function jsonPathPrefix<Prefix extends string, Path extends string>(prefix: Prefix, path: Path): Path extends StringConcatOf<'$', infer ToMount> ? `${Prefix}${ToMount}` : never;
|
|
305
|
+
declare function jsonPathUnprefix<Prefix extends string, Path extends string>(prefix: Prefix, path: Path): Path extends StringConcatOf<Prefix, infer ToUnmount> ? `$${ToUnmount}` : never;
|
|
303
306
|
|
|
304
307
|
declare function valuePathToTypePath<ValuePathsToTypePaths extends Record<string, string>, ValuePath extends keyof ValuePathsToTypePaths>({ definition: typeDef }: Type, valuePath: ValuePath, allowMissingPaths?: boolean): ValuePathsToTypePaths[ValuePath];
|
|
305
308
|
|
|
@@ -592,4 +595,4 @@ type ValidatorsOfValues<FlattenedValues extends Readonly<Record<string, any>>, T
|
|
|
592
595
|
readonly [K in keyof FlattenedValues]: Validator<FlattenedValues[K], any, TypePathsToValuePaths[K], Context>;
|
|
593
596
|
};
|
|
594
597
|
|
|
595
|
-
export { type Accessor, type AnnotatedValidator, type Annotations, type AnyValueType, DefinedValidator, type ErrorOfValidator, type FlattenedAccessorsOfType, type FlattenedTypesOfType, type FlattenedValuesOfType, type FunctionalValidator, type InternalJsonPathsOf, type IsStrictUnion, type ListTypeDef, type LiteralTypeDef, type Mapper, type MinimumStringLengthValidationError, MinimumStringLengthValidationErrorType, MinimumStringLengthValidator, type MobxObservable, type MobxValueOfType, type NonMobxObservable, type ObjectFieldKey, type ObjectTypeDef, type ObjectTypeDefFields, OptionalValidatorProxy, type PathsOfType, type ReadonlyOfTypeDef, type ReadonlyTypeOfType, type RecordKeyType, type RecordTypeDef, type RegexpValidationError, RegexpValidationErrorType, RegexpValidator, type Setter, type StrictListTypeDef, type StrictLiteralTypeDef, type StrictObjectTypeDef, type StrictObjectTypeDefFields, type StrictRecordTypeDef, type StrictType, type StrictTypeDef, type StrictUnionTypeDef, type Type, type TypeDef, TypeDefType, type UnionKey, type UnionTypeDef, type ValidationError, type Validator, type ValidatorsOfValues, type ValueOfType, type ValueOfTypeDef, type ValueToTypePathsOfType, type ValueTypesOfDiscriminatedUnion, annotations, booleanType, copy, flattenAccessorsOfType, flattenJsonValueToTypePathsOf, flattenTypesOfType, flattenValidatorsOfValidatingType, flattenValueTo, flattenValuesOfType, getUnionTypeDef, isAnnotatedValidator, isFunctionalValidator, jsonPath, jsonPathPop, list, literal, mergeAnnotations, mobxCopy, nullType, nullable, numberType, object, record, stringType, union, validate, valuePathToTypePath };
|
|
598
|
+
export { type Accessor, type AnnotatedValidator, type Annotations, type AnyValueType, DefinedValidator, type ErrorOfValidator, type FlattenedAccessorsOfType, type FlattenedTypesOfType, type FlattenedValuesOfType, type FunctionalValidator, type InternalJsonPathsOf, type IsStrictUnion, type ListTypeDef, type LiteralTypeDef, type Mapper, type MinimumStringLengthValidationError, MinimumStringLengthValidationErrorType, MinimumStringLengthValidator, type MobxObservable, type MobxValueOfType, type NonMobxObservable, type ObjectFieldKey, type ObjectTypeDef, type ObjectTypeDefFields, OptionalValidatorProxy, type PathsOfType, type ReadonlyOfTypeDef, type ReadonlyTypeOfType, type RecordKeyType, type RecordTypeDef, type RegexpValidationError, RegexpValidationErrorType, RegexpValidator, type Setter, type StrictListTypeDef, type StrictLiteralTypeDef, type StrictObjectTypeDef, type StrictObjectTypeDefFields, type StrictRecordTypeDef, type StrictType, type StrictTypeDef, type StrictUnionTypeDef, type Type, type TypeDef, TypeDefType, type UnionKey, type UnionTypeDef, type ValidationError, type Validator, type ValidatorsOfValues, type ValueOfType, type ValueOfTypeDef, type ValueToTypePathsOfType, type ValueTypesOfDiscriminatedUnion, annotations, booleanType, copy, flattenAccessorsOfType, flattenJsonValueToTypePathsOf, flattenTypesOfType, flattenValidatorsOfValidatingType, flattenValueTo, flattenValuesOfType, getUnionTypeDef, isAnnotatedValidator, isFunctionalValidator, jsonPath, jsonPathPop, jsonPathPrefix, jsonPathUnprefix, list, literal, mergeAnnotations, mobxCopy, nullType, nullable, numberType, object, record, stringType, union, validate, valuePathToTypePath };
|
package/dist/index.js
CHANGED
|
@@ -185,8 +185,12 @@ import {
|
|
|
185
185
|
} from "@strictly/base";
|
|
186
186
|
|
|
187
187
|
// transformers/flatteners/json_path.ts
|
|
188
|
-
|
|
189
|
-
|
|
188
|
+
import {
|
|
189
|
+
assertEqual,
|
|
190
|
+
assertState
|
|
191
|
+
} from "@strictly/base";
|
|
192
|
+
function jsonPath(prefix, segment, qualifier) {
|
|
193
|
+
const s = `.${qualifier != null ? qualifier : ""}${segment}`;
|
|
190
194
|
return `${prefix}${s}`;
|
|
191
195
|
}
|
|
192
196
|
function jsonPathPop(path) {
|
|
@@ -199,6 +203,14 @@ function jsonPathPop(path) {
|
|
|
199
203
|
parts.pop()
|
|
200
204
|
];
|
|
201
205
|
}
|
|
206
|
+
function jsonPathPrefix(prefix, path) {
|
|
207
|
+
assertEqual(path[0], "$", "{} should start with $", path);
|
|
208
|
+
return `${prefix}${path.slice(1)}`;
|
|
209
|
+
}
|
|
210
|
+
function jsonPathUnprefix(prefix, path) {
|
|
211
|
+
assertState(path.startsWith(prefix), "{} should start with {}", path, prefix);
|
|
212
|
+
return `$${path.slice(prefix.length)}`;
|
|
213
|
+
}
|
|
202
214
|
|
|
203
215
|
// transformers/flatteners/flatten_value_to.ts
|
|
204
216
|
function flattenValueTo({ definition }, v, setter, mapper2) {
|
|
@@ -523,7 +535,7 @@ function flattenValuesOfType(typeDef, value) {
|
|
|
523
535
|
|
|
524
536
|
// transformers/flatteners/value_path_to_type_path.ts
|
|
525
537
|
import {
|
|
526
|
-
assertEqual,
|
|
538
|
+
assertEqual as assertEqual2,
|
|
527
539
|
assertExists,
|
|
528
540
|
assertExistsAndReturn,
|
|
529
541
|
PreconditionFailedError,
|
|
@@ -532,7 +544,7 @@ import {
|
|
|
532
544
|
} from "@strictly/base";
|
|
533
545
|
function valuePathToTypePath({ definition: typeDef }, valuePath, allowMissingPaths = false) {
|
|
534
546
|
const valueSteps = valuePath.split(/\.|\[/g);
|
|
535
|
-
|
|
547
|
+
assertEqual2(valueSteps[0], "$");
|
|
536
548
|
const typeSteps = internalJsonValuePathToTypePath(
|
|
537
549
|
typeDef,
|
|
538
550
|
valueSteps.slice(1),
|
|
@@ -1107,6 +1119,8 @@ export {
|
|
|
1107
1119
|
isFunctionalValidator,
|
|
1108
1120
|
jsonPath,
|
|
1109
1121
|
jsonPathPop,
|
|
1122
|
+
jsonPathPrefix,
|
|
1123
|
+
jsonPathUnprefix,
|
|
1110
1124
|
list,
|
|
1111
1125
|
literal,
|
|
1112
1126
|
mergeAnnotations,
|
package/package.json
CHANGED
|
@@ -1,9 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
assertEqual,
|
|
3
|
+
assertState,
|
|
4
|
+
type StringConcatOf,
|
|
5
|
+
} from '@strictly/base'
|
|
6
|
+
|
|
7
|
+
export function jsonPath<
|
|
8
|
+
Prefix extends string,
|
|
9
|
+
Segment extends number | string,
|
|
10
|
+
>(prefix: Prefix, segment: Segment): `${Prefix}.${Segment}`
|
|
11
|
+
export function jsonPath<
|
|
12
|
+
Prefix extends string,
|
|
13
|
+
Segment extends number | string,
|
|
14
|
+
Qualifier extends string,
|
|
15
|
+
>(prefix: Prefix, segment: Segment, qualifier: Qualifier): `${Prefix}.${Qualifier}${Segment}`
|
|
16
|
+
export function jsonPath<
|
|
17
|
+
Prefix extends string,
|
|
18
|
+
Segment extends number | string,
|
|
19
|
+
Qualifier extends string = '',
|
|
20
|
+
>(prefix: Prefix, segment: Segment, qualifier?: Qualifier) {
|
|
21
|
+
const s = `.${qualifier ?? ''}${segment}`
|
|
22
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
23
|
+
return `${prefix}${s}` as `${Prefix}.${Qualifier}${Segment}`
|
|
4
24
|
}
|
|
5
25
|
|
|
6
|
-
|
|
26
|
+
// TODO type safety
|
|
27
|
+
export function jsonPathPop<Path extends string>(path: Path): [string, string] | null {
|
|
7
28
|
const parts = path.split('.')
|
|
8
29
|
if (parts.length <= 1) {
|
|
9
30
|
return null
|
|
@@ -13,3 +34,22 @@ export function jsonPathPop(path: string): [string, string] | null {
|
|
|
13
34
|
parts.pop()!,
|
|
14
35
|
]
|
|
15
36
|
}
|
|
37
|
+
|
|
38
|
+
export function jsonPathPrefix<
|
|
39
|
+
Prefix extends string,
|
|
40
|
+
Path extends string,
|
|
41
|
+
>(prefix: Prefix, path: Path): Path extends StringConcatOf<'$', infer ToMount> ? `${Prefix}${ToMount}` : never {
|
|
42
|
+
assertEqual(path[0], '$', '{} should start with $', path)
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
44
|
+
return `${prefix}${path.slice(1)}` as Path extends StringConcatOf<'$', infer ToMount> ? `${Prefix}${ToMount}` : never
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function jsonPathUnprefix<
|
|
48
|
+
Prefix extends string,
|
|
49
|
+
Path extends string,
|
|
50
|
+
>(prefix: Prefix, path: Path): Path extends StringConcatOf<Prefix, infer ToUnmount> ? `$${ToUnmount}` : never {
|
|
51
|
+
assertState(path.startsWith(prefix), '{} should start with {}', path, prefix)
|
|
52
|
+
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
|
|
53
|
+
return `$${path.slice(prefix.length)}` as Path extends StringConcatOf<Prefix, infer ToUnmount> ? `$${ToUnmount}`
|
|
54
|
+
: never
|
|
55
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {
|
|
2
|
+
jsonPath,
|
|
3
|
+
jsonPathPrefix,
|
|
4
|
+
jsonPathUnprefix,
|
|
5
|
+
} from 'transformers/flatteners/json_path'
|
|
6
|
+
|
|
7
|
+
describe('json_paths', () => {
|
|
8
|
+
describe('jsonPath', () => {
|
|
9
|
+
describe('simple', () => {
|
|
10
|
+
const path = jsonPath('$', 'thing')
|
|
11
|
+
|
|
12
|
+
it('has the expected type', () => {
|
|
13
|
+
expectTypeOf(path).toEqualTypeOf<'$.thing'>()
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
it('has the expected value', () => {
|
|
17
|
+
expect(path).toEqual('$.thing')
|
|
18
|
+
})
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
describe('indexed', () => {
|
|
22
|
+
const path = jsonPath<`$.${number}`, number>(`$.${1}`, 1)
|
|
23
|
+
|
|
24
|
+
it('has the expected type', () => {
|
|
25
|
+
expectTypeOf(path).toEqualTypeOf<`$.${number}.${number}`>()
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
it('has the expected value', () => {
|
|
29
|
+
expect(path).toEqual('$.1.1')
|
|
30
|
+
})
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
describe('qualified', () => {
|
|
34
|
+
const path = jsonPath('$', 'x', 'y:')
|
|
35
|
+
|
|
36
|
+
it('has the expected type', () => {
|
|
37
|
+
expectTypeOf(path).toEqualTypeOf<'$.y:x'>()
|
|
38
|
+
})
|
|
39
|
+
|
|
40
|
+
it('has the expected value', () => {
|
|
41
|
+
expect(path).toEqual('$.y:x')
|
|
42
|
+
})
|
|
43
|
+
})
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
describe('jsonPathPrefix', () => {
|
|
47
|
+
const path = jsonPathPrefix('$.x', '$.x.y')
|
|
48
|
+
|
|
49
|
+
it('has the expected type', () => {
|
|
50
|
+
expectTypeOf(path).toEqualTypeOf<'$.x.x.y'>()
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
it('has the expected value', () => {
|
|
54
|
+
expect(path).toEqual('$.x.x.y')
|
|
55
|
+
})
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
describe('jsonPathUnprefix', () => {
|
|
59
|
+
const path = jsonPathUnprefix('$.x.x.x', '$.x.x.x.x.y')
|
|
60
|
+
|
|
61
|
+
it('has the expected type', () => {
|
|
62
|
+
expectTypeOf(path).toEqualTypeOf<'$.x.y'>()
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
it('has the expected value', () => {
|
|
66
|
+
expect(path).toEqual('$.x.y')
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
})
|