@nudojs/core 2.0.1 → 2.1.0
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/index.d.ts +8 -6
- package/dist/index.js +23 -13
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -799,23 +799,25 @@ declare function string(): ConstraintBuilder;
|
|
|
799
799
|
declare function boolean(): ConstraintBuilder;
|
|
800
800
|
/** any() —— 无约束;formatConstraint / draft import 与显示同源 */
|
|
801
801
|
declare function any(): ConstraintBuilder;
|
|
802
|
-
/** array(item) —— 数组,元素满足 item */
|
|
803
|
-
declare function array(item: NudoConstraint | ConstraintBuilder): ConstraintBuilder;
|
|
802
|
+
/** array(item) —— 数组,元素满足 item;item 亦可为具体字面量(指令文法) */
|
|
803
|
+
declare function array(item: NudoConstraint | ConstraintBuilder | number | string | boolean | null | undefined): ConstraintBuilder;
|
|
804
804
|
/**
|
|
805
805
|
* object 形状约束(契约规范形状,无需 interface):
|
|
806
806
|
*
|
|
807
807
|
* shape({ id: number().gt(0), name: string() })
|
|
808
|
+
*
|
|
809
|
+
* 字段值亦可为具体字面量(指令文法)。
|
|
808
810
|
*/
|
|
809
|
-
declare function shape(fields: Record<string, NudoConstraint | ConstraintBuilder>): ConstraintBuilder;
|
|
811
|
+
declare function shape(fields: Record<string, NudoConstraint | ConstraintBuilder | number | string | boolean | null | undefined>): ConstraintBuilder;
|
|
810
812
|
/** union(...cs):成员析取;instantiate 为 or(...),entry Abs 为成员 joinAbs。空参 throw */
|
|
811
|
-
declare function union(...cs: (NudoConstraint | ConstraintBuilder)[]): ConstraintBuilder;
|
|
813
|
+
declare function union(...cs: (NudoConstraint | ConstraintBuilder | number | string | boolean | null | undefined)[]): ConstraintBuilder;
|
|
812
814
|
/**
|
|
813
815
|
* fn(params, returns?, { throws? }):一等函数约束。
|
|
814
816
|
* Phase 1 只展示不执法:参数位 instantiate 恒真(pTrue),
|
|
815
817
|
* 逐参约束经 fnConstraintToEntryReqs 消费。
|
|
816
818
|
*/
|
|
817
|
-
declare function fn(params: Record<string, NudoConstraint | ConstraintBuilder>, returns?: NudoConstraint | ConstraintBuilder, opts?: {
|
|
818
|
-
throws?: NudoConstraint | ConstraintBuilder;
|
|
819
|
+
declare function fn(params: Record<string, NudoConstraint | ConstraintBuilder | number | string | boolean | null | undefined>, returns?: NudoConstraint | ConstraintBuilder | number | string | boolean | null | undefined, opts?: {
|
|
820
|
+
throws?: NudoConstraint | ConstraintBuilder | number | string | boolean | null | undefined;
|
|
819
821
|
}): ConstraintBuilder;
|
|
820
822
|
/** partial(c):shape 全字段变可选;非 shape throw */
|
|
821
823
|
declare function partial(c: NudoConstraint | ConstraintBuilder): ConstraintBuilder;
|
package/dist/index.js
CHANGED
|
@@ -666,20 +666,17 @@ function any() {
|
|
|
666
666
|
return makeBuilder(void 0, []);
|
|
667
667
|
}
|
|
668
668
|
function array(item) {
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
669
|
+
return makeBuilder(void 0, [], {
|
|
670
|
+
element: asNestedConstraint(item, "array(item)")
|
|
671
|
+
});
|
|
672
672
|
}
|
|
673
673
|
function shape(fields) {
|
|
674
674
|
const mapped = {};
|
|
675
675
|
for (const [k, v2] of Object.entries(fields)) {
|
|
676
|
-
|
|
677
|
-
throw new Error(
|
|
678
|
-
`nudo: shape \u5B57\u6BB5 '${k}' \u671F\u671B\u7EA6\u675F\u503C\uFF08number()/string()/\u2026\u6216\u5176\u7EC4\u5408\u5B50\uFF09\uFF0C\u6536\u5230\u975E\u7EA6\u675F`
|
|
679
|
-
);
|
|
676
|
+
const constraint = asNestedConstraint(v2, `shape \u5B57\u6BB5 '${k}'`);
|
|
680
677
|
mapped[k] = {
|
|
681
|
-
constraint
|
|
682
|
-
...
|
|
678
|
+
constraint,
|
|
679
|
+
...constraint.isOptional ? { optional: true } : {}
|
|
683
680
|
};
|
|
684
681
|
}
|
|
685
682
|
return makeBuilder(void 0, [], { fields: mapped });
|
|
@@ -704,19 +701,32 @@ function lit2(v2) {
|
|
|
704
701
|
const prim = typeof v2 === "number" ? "number" : typeof v2 === "string" ? "string" : typeof v2 === "boolean" ? "boolean" : void 0;
|
|
705
702
|
return makeBuilder(prim, [eq(selfTerm(), lit(v2))]);
|
|
706
703
|
}
|
|
704
|
+
function asNestedConstraint(x, ctx) {
|
|
705
|
+
if (isConstraint(x)) return toPlainConstraint(x);
|
|
706
|
+
if (x === null || x === void 0 || typeof x === "number" || typeof x === "string" || typeof x === "boolean") {
|
|
707
|
+
return toPlainConstraint(lit2(x));
|
|
708
|
+
}
|
|
709
|
+
throw new Error(
|
|
710
|
+
`nudo: ${ctx} \u671F\u671B\u7EA6\u675F\u503C\uFF08number()/string()/\u2026\uFF09\u6216\u5177\u4F53\u5B57\u9762\u91CF\uFF0C\u6536\u5230\u975E\u7EA6\u675F`
|
|
711
|
+
);
|
|
712
|
+
}
|
|
707
713
|
function union(...cs) {
|
|
708
714
|
if (cs.length === 0)
|
|
709
715
|
throw new Error("nudo union(): \u81F3\u5C11\u9700\u8981\u4E00\u4E2A\u6210\u5458\u7EA6\u675F");
|
|
710
|
-
return makeBuilder(void 0, [], {
|
|
716
|
+
return makeBuilder(void 0, [], {
|
|
717
|
+
members: cs.map((c) => asNestedConstraint(c, "union \u6210\u5458"))
|
|
718
|
+
});
|
|
711
719
|
}
|
|
712
720
|
function fn(params, returns, opts) {
|
|
713
721
|
const normalized = {};
|
|
714
|
-
for (const [k, v2] of Object.entries(params))
|
|
722
|
+
for (const [k, v2] of Object.entries(params)) {
|
|
723
|
+
normalized[k] = asNestedConstraint(v2, `fn \u53C2\u6570 '${k}'`);
|
|
724
|
+
}
|
|
715
725
|
return makeBuilder(void 0, [], {
|
|
716
726
|
fn: {
|
|
717
727
|
params: normalized,
|
|
718
|
-
...returns !== void 0 ? { returns:
|
|
719
|
-
...opts?.throws !== void 0 ? { throws:
|
|
728
|
+
...returns !== void 0 ? { returns: asNestedConstraint(returns, "fn \u8FD4\u56DE\u503C") } : {},
|
|
729
|
+
...opts?.throws !== void 0 ? { throws: asNestedConstraint(opts.throws, "fn throws") } : {}
|
|
720
730
|
}
|
|
721
731
|
});
|
|
722
732
|
}
|