@arcblock/validator 1.29.26 → 1.30.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/esm/index.d.mts CHANGED
@@ -1,55 +1,13 @@
1
- import { BNSchema } from "./extension/bn.mjs";
2
- import { DIDSchema } from "./extension/did.mjs";
3
- import BaseJoi, { Root } from "joi";
1
+ import { vDID } from "./v/did.mjs";
2
+ import { vBN, vBNGreater, vBNGt, vBNGte, vBNLess, vBNLt, vBNLte, vBNMax, vBNMin, vBNNegative, vBNPositive } from "./v/bn.mjs";
3
+ import { vFilteredRecord, vForbidden, vStripUnknown, vValidate, vWhen } from "./v/helpers.mjs";
4
+ import { schemas } from "./v/schemas.mjs";
5
+ import "./v/index.mjs";
6
+ import * as v from "valibot";
4
7
 
5
8
  //#region src/index.d.ts
6
- interface ExtendedRoot extends Root {
7
- DID(): DIDSchema;
8
- BN(): BNSchema;
9
- }
10
- declare const Joi: ExtendedRoot;
11
- declare const schemas: {
12
- context: BaseJoi.ObjectSchema<any>;
13
- tokenInput: BaseJoi.ObjectSchema<any>;
14
- multiInput: BaseJoi.ArraySchema<any[]>;
15
- multiSig: BaseJoi.ArraySchema<any[]>;
16
- foreignToken: BaseJoi.ObjectSchema<any>;
17
- variableInput: BaseJoi.ObjectSchema<any>;
18
- nftDisplay: BaseJoi.ObjectSchema<any>;
19
- nftEndpoint: BaseJoi.ObjectSchema<any>;
20
- nftIssuer: BaseJoi.ObjectSchema<any>;
21
- tokenHolder: BaseJoi.AlternativesSchema<any>;
22
- assetProps: {
23
- address: DIDSchema;
24
- moniker: BaseJoi.StringSchema<string>;
25
- data: BaseJoi.AnySchema<any>;
26
- readonly: BaseJoi.BooleanSchema<boolean>;
27
- transferrable: BaseJoi.BooleanSchema<boolean>;
28
- ttl: BaseJoi.NumberSchema<number>;
29
- parent: DIDSchema;
30
- issuer: DIDSchema;
31
- endpoint: BaseJoi.ObjectSchema<any>;
32
- display: BaseJoi.ObjectSchema<any>;
33
- tags: BaseJoi.ArraySchema<any[]>;
34
- };
35
- assetSchema: BaseJoi.ObjectSchema<any>;
36
- factoryProps: {
37
- address: DIDSchema;
38
- name: BaseJoi.StringSchema<string>;
39
- description: BaseJoi.StringSchema<string>;
40
- settlement: BaseJoi.StringSchema<string>;
41
- limit: BaseJoi.NumberSchema<number>;
42
- trustedIssuers: BaseJoi.ArraySchema<any[]>;
43
- data: BaseJoi.AnySchema<any>;
44
- display: BaseJoi.ObjectSchema<any>;
45
- input: BaseJoi.ObjectSchema<any>;
46
- output: BaseJoi.ObjectSchema<any>;
47
- hooks: BaseJoi.ArraySchema<any[]>;
48
- };
49
- factorySchema: BaseJoi.ObjectSchema<any>;
50
- };
51
9
  declare const patterns: {
52
10
  txHash: RegExp;
53
11
  };
54
12
  //#endregion
55
- export { ExtendedRoot, Joi, patterns, schemas };
13
+ export { patterns, schemas, v, vBN, vBNGreater, vBNGt, vBNGte, vBNLess, vBNLt, vBNLte, vBNMax, vBNMin, vBNNegative, vBNPositive, vDID, vFilteredRecord, vForbidden, vStripUnknown, vValidate, vWhen };
package/esm/index.mjs CHANGED
@@ -1,139 +1,11 @@
1
- import { BNExtension } from "./extension/bn.mjs";
2
- import { DIDExtension } from "./extension/did.mjs";
3
- import BaseJoi from "joi";
1
+ import { vDID } from "./v/did.mjs";
2
+ import { vBN, vBNGreater, vBNGt, vBNGte, vBNLess, vBNLt, vBNLte, vBNMax, vBNMin, vBNNegative, vBNPositive } from "./v/bn.mjs";
3
+ import { vFilteredRecord, vForbidden, vStripUnknown, vValidate, vWhen } from "./v/helpers.mjs";
4
+ import { schemas } from "./v/schemas.mjs";
5
+ import * as v from "valibot";
4
6
 
5
7
  //#region src/index.ts
6
- const Joi = BaseJoi.extend(DIDExtension).extend(BNExtension);
7
- const txHash = /^(0x)?([A-Fa-f0-9]{64})$/;
8
- const context = Joi.object({
9
- genesisTime: Joi.date().iso().required().raw(),
10
- genesisTx: Joi.string().regex(txHash).required().allow(""),
11
- renaissanceTime: Joi.date().iso().required().raw(),
12
- renaissanceTx: Joi.string().regex(txHash).required().allow("")
13
- });
14
- const tokenInput = Joi.object({
15
- address: Joi.DID().prefix().role("ROLE_TOKEN").required(),
16
- value: Joi.BN().min(0).required()
17
- });
18
- const variableInput = Joi.object({
19
- name: Joi.string().min(1).max(256).required(),
20
- value: Joi.string().min(1).max(1024).required(),
21
- description: Joi.string().min(1).max(256).optional().allow(""),
22
- required: Joi.boolean().optional().default(false)
23
- });
24
- const tokenHolder = Joi.alternatives().try(Joi.DID().prefix().role("ROLE_ACCOUNT"), Joi.DID().prefix().role("ROLE_APPLICATION"), Joi.DID().prefix().role("ROLE_BLOCKLET"), Joi.DID().prefix().wallet("ethereum"), Joi.DID().prefix().wallet("passkey"));
25
- const multiInput = Joi.array().items(Joi.object({
26
- owner: tokenHolder.required(),
27
- tokensList: Joi.array().items(tokenInput).default([]),
28
- assetsList: Joi.array().items(Joi.DID().prefix().role("ROLE_ASSET")).default([])
29
- }));
30
- const multiSig = Joi.array().items({
31
- signer: Joi.DID().prefix().required(),
32
- pk: Joi.any().required(),
33
- signature: Joi.any().required(),
34
- delegator: Joi.DID().prefix().valid("").optional(),
35
- data: Joi.any().optional()
36
- });
37
- const foreignToken = Joi.object({
38
- type: Joi.string().min(1).max(32).required(),
39
- contractAddress: Joi.DID().prefix().wallet("ethereum").required(),
40
- chainType: Joi.string().min(1).max(32).required(),
41
- chainName: Joi.string().min(1).max(32).required(),
42
- chainId: Joi.number().positive().required()
43
- });
44
- const nftDisplay = Joi.object({
45
- type: Joi.string().valid("svg", "url", "uri").required(),
46
- content: Joi.string().when("type", {
47
- is: "uri",
48
- then: Joi.string().max(20480).dataUri().required()
49
- }).when("type", {
50
- is: "url",
51
- then: Joi.string().max(2048).uri({ scheme: ["http", "https"] }).required()
52
- })
53
- });
54
- const nftEndpoint = Joi.object({
55
- id: Joi.string().max(2048).uri({ scheme: ["http", "https"] }).required(),
56
- scope: Joi.string().valid("public", "private").default("public")
57
- });
58
- const nftIssuer = Joi.object({
59
- id: Joi.DID().prefix().required(),
60
- pk: Joi.string().required(),
61
- name: Joi.string().min(1).max(256).required()
62
- });
63
- const assetProps = {
64
- address: Joi.DID().prefix().role("ROLE_ASSET").required(),
65
- moniker: Joi.string().min(2).max(255).required(),
66
- data: Joi.any().required(),
67
- readonly: Joi.boolean().default(false),
68
- transferrable: Joi.boolean().default(false),
69
- ttl: Joi.number().min(0).default(0),
70
- parent: Joi.DID().prefix().optional().allow(""),
71
- issuer: Joi.DID().prefix().optional().allow(""),
72
- endpoint: nftEndpoint.optional().allow(null).default(null),
73
- display: nftDisplay.optional().allow(null).default(null),
74
- tags: Joi.array().items(Joi.string().min(1).max(64)).max(4).optional()
75
- };
76
- const factoryProps = {
77
- address: Joi.DID().prefix().role("ROLE_FACTORY").required(),
78
- name: Joi.string().min(2).max(255).required(),
79
- description: Joi.string().min(2).max(255).required(),
80
- settlement: Joi.string().valid("instant", "periodic").default("instant"),
81
- limit: Joi.number().integer().min(0).default(0),
82
- trustedIssuers: Joi.array().items(Joi.DID().prefix()).max(8).optional(),
83
- data: Joi.any().optional().allow(null).default(null),
84
- display: nftDisplay.optional().allow(null).default(null),
85
- input: Joi.object({
86
- value: Joi.BN().positive().min("0").default("0"),
87
- tokens: Joi.array().items(tokenInput).max(8).optional(),
88
- assets: Joi.array().items(Joi.alternatives().try(Joi.DID().prefix().role("ROLE_ASSET"), Joi.DID().prefix().role("ROLE_FACTORY"))).max(8).optional(),
89
- variables: Joi.array().items(Joi.object({
90
- name: Joi.string().min(1).max(255).required(),
91
- description: Joi.string().max(255).optional().allow(""),
92
- required: Joi.boolean().required()
93
- })).optional()
94
- }),
95
- output: Joi.object({
96
- moniker: Joi.string().min(2).max(255).required(),
97
- data: Joi.any().required(),
98
- readonly: Joi.boolean().default(false),
99
- transferrable: Joi.boolean().default(false),
100
- ttl: Joi.number().min(0).default(0),
101
- parent: Joi.string().max(255).required(),
102
- issuer: Joi.string().max(255).required(),
103
- endpoint: nftEndpoint.optional(),
104
- display: nftDisplay.optional(),
105
- tags: Joi.array().items(Joi.string().min(1).max(64)).max(4).optional()
106
- }),
107
- hooks: Joi.array().items(Joi.object({
108
- name: Joi.string().valid("mint", "postMint", "preMint").required(),
109
- type: Joi.string().valid("url", "contract").required(),
110
- hook: Joi.string().min(1).max(4096).required(),
111
- compiled: Joi.array().items(Joi.object()).optional()
112
- })).optional()
113
- };
114
- const schemas = {
115
- context,
116
- tokenInput,
117
- multiInput,
118
- multiSig,
119
- foreignToken,
120
- variableInput,
121
- nftDisplay,
122
- nftEndpoint,
123
- nftIssuer,
124
- tokenHolder,
125
- assetProps,
126
- assetSchema: Joi.object(assetProps).options({
127
- stripUnknown: true,
128
- noDefaults: false
129
- }),
130
- factoryProps,
131
- factorySchema: Joi.object(factoryProps).options({
132
- stripUnknown: true,
133
- noDefaults: false
134
- })
135
- };
136
- const patterns = { txHash };
8
+ const patterns = { txHash: /^(0x)?([A-Fa-f0-9]{64})$/ };
137
9
 
138
10
  //#endregion
139
- export { Joi, patterns, schemas };
11
+ export { patterns, schemas, v, vBN, vBNGreater, vBNGt, vBNGte, vBNLess, vBNLt, vBNLte, vBNMax, vBNMin, vBNNegative, vBNPositive, vDID, vFilteredRecord, vForbidden, vStripUnknown, vValidate, vWhen };
package/esm/v/bn.d.mts ADDED
@@ -0,0 +1,16 @@
1
+ import * as v from "valibot";
2
+
3
+ //#region src/v/bn.d.ts
4
+ declare const vBNMin: (t: any) => v.CheckAction<any, `Value must be >= ${string}` | `Value must be <= ${string}` | `Value must be > ${string}` | `Value must be < ${string}`>;
5
+ declare const vBNMax: (t: any) => v.CheckAction<any, `Value must be >= ${string}` | `Value must be <= ${string}` | `Value must be > ${string}` | `Value must be < ${string}`>;
6
+ declare const vBNGreater: (t: any) => v.CheckAction<any, `Value must be >= ${string}` | `Value must be <= ${string}` | `Value must be > ${string}` | `Value must be < ${string}`>;
7
+ declare const vBNLess: (t: any) => v.CheckAction<any, `Value must be >= ${string}` | `Value must be <= ${string}` | `Value must be > ${string}` | `Value must be < ${string}`>;
8
+ declare const vBNPositive: () => v.CheckAction<any, `Value must be >= ${string}` | `Value must be <= ${string}` | `Value must be > ${string}` | `Value must be < ${string}`>;
9
+ declare const vBNNegative: () => v.CheckAction<any, `Value must be >= ${string}` | `Value must be <= ${string}` | `Value must be > ${string}` | `Value must be < ${string}`>;
10
+ declare const vBNGte: (t: any) => v.CheckAction<any, `Value must be >= ${string}` | `Value must be <= ${string}` | `Value must be > ${string}` | `Value must be < ${string}`>;
11
+ declare const vBNLte: (t: any) => v.CheckAction<any, `Value must be >= ${string}` | `Value must be <= ${string}` | `Value must be > ${string}` | `Value must be < ${string}`>;
12
+ declare const vBNGt: (t: any) => v.CheckAction<any, `Value must be >= ${string}` | `Value must be <= ${string}` | `Value must be > ${string}` | `Value must be < ${string}`>;
13
+ declare const vBNLt: (t: any) => v.CheckAction<any, `Value must be >= ${string}` | `Value must be <= ${string}` | `Value must be > ${string}` | `Value must be < ${string}`>;
14
+ declare function vBN(...actions: any[]): any;
15
+ //#endregion
16
+ export { vBN, vBNGreater, vBNGt, vBNGte, vBNLess, vBNLt, vBNLte, vBNMax, vBNMin, vBNNegative, vBNPositive };
package/esm/v/bn.mjs ADDED
@@ -0,0 +1,54 @@
1
+ import * as v from "valibot";
2
+ import BN from "bn.js";
3
+
4
+ //#region src/v/bn.ts
5
+ const BN_ZERO = new BN(0);
6
+ const bnOps = {
7
+ gte: (a, b) => a.gte(b),
8
+ lte: (a, b) => a.lte(b),
9
+ gt: (a, b) => a.gt(b),
10
+ lt: (a, b) => a.lt(b)
11
+ };
12
+ const opLabels = {
13
+ gte: ">=",
14
+ lte: "<=",
15
+ gt: ">",
16
+ lt: "<"
17
+ };
18
+ function bnCompare(op, threshold) {
19
+ const t = BN.isBN(threshold) ? threshold : new BN(threshold);
20
+ return v.check((value) => bnOps[op](value, t), `Value must be ${opLabels[op]} ${t.toString(10)}`);
21
+ }
22
+ const vBNMin = (t) => bnCompare("gte", t);
23
+ const vBNMax = (t) => bnCompare("lte", t);
24
+ const vBNGreater = (t) => bnCompare("gt", t);
25
+ const vBNLess = (t) => bnCompare("lt", t);
26
+ const vBNPositive = () => bnCompare("gt", BN_ZERO);
27
+ const vBNNegative = () => bnCompare("lt", BN_ZERO);
28
+ const vBNGte = vBNMin;
29
+ const vBNLte = vBNMax;
30
+ const vBNGt = vBNGreater;
31
+ const vBNLt = vBNLess;
32
+ function safeBN(input) {
33
+ if (input === null || input === void 0 || typeof input === "number" && Number.isNaN(input)) return false;
34
+ try {
35
+ const bn = new BN(input);
36
+ bn.toString(10);
37
+ return bn;
38
+ } catch {
39
+ return false;
40
+ }
41
+ }
42
+ function vBN(...actions) {
43
+ const pipeItems = [
44
+ v.any(),
45
+ v.check((input) => safeBN(input) !== false, "is not a big number"),
46
+ v.transform((input) => safeBN(input)),
47
+ ...actions,
48
+ v.transform((value) => value.toString(10))
49
+ ];
50
+ return v.pipe(...pipeItems);
51
+ }
52
+
53
+ //#endregion
54
+ export { vBN, vBNGreater, vBNGt, vBNGte, vBNLess, vBNLt, vBNLte, vBNMax, vBNMin, vBNNegative, vBNPositive };
@@ -0,0 +1,14 @@
1
+ import { DIDTypeShortcut } from "@arcblock/did";
2
+ import { HashType, KeyType, RoleType } from "@ocap/mcrypto";
3
+
4
+ //#region src/v/did.d.ts
5
+ interface VDIDOptions {
6
+ prefix?: boolean | string;
7
+ role?: RoleType;
8
+ wallet?: DIDTypeShortcut;
9
+ pk?: KeyType;
10
+ hash?: HashType;
11
+ }
12
+ declare function vDID(opts?: VDIDOptions): any;
13
+ //#endregion
14
+ export { VDIDOptions, vDID };
package/esm/v/did.mjs ADDED
@@ -0,0 +1,41 @@
1
+ import * as v from "valibot";
2
+ import { DID_TYPE_ARCBLOCK, DID_TYPE_ETHEREUM, DID_TYPE_PASSKEY, isKnownDid, toAddress, toTypeInfo, toTypeInfoStr } from "@arcblock/did";
3
+
4
+ //#region src/v/did.ts
5
+ const isEqual = (a, b) => {
6
+ const aKeys = Object.keys(a);
7
+ const bKeys = Object.keys(b);
8
+ if (aKeys.length !== bKeys.length) return false;
9
+ return aKeys.every((k) => a[k] === b[k]);
10
+ };
11
+ function vDID(opts = {}) {
12
+ const pipeItems = [
13
+ v.string(),
14
+ v.trim(),
15
+ v.check((value) => {
16
+ return !!value && isKnownDid(value) !== false;
17
+ }, "Expected value to be a valid DID")
18
+ ];
19
+ if (opts.prefix !== void 0 && opts.prefix !== false) {
20
+ if (opts.prefix === true || opts.prefix === "") pipeItems.push(v.check((value) => toAddress(value) === value, "Expected value to have no DID prefix"));
21
+ else if (typeof opts.prefix === "string") pipeItems.push(v.check((value) => value.startsWith(opts.prefix), `Expected DID prefix to be "${opts.prefix}"`));
22
+ }
23
+ if (opts.role || opts.pk || opts.hash) pipeItems.push(v.check((value) => {
24
+ const t = toTypeInfoStr(value);
25
+ if (opts.role && t.role !== opts.role) return false;
26
+ if (opts.pk && t.pk !== opts.pk) return false;
27
+ if (opts.hash && t.hash !== opts.hash) return false;
28
+ return true;
29
+ }, "Expected DID type info to match specified constraints"));
30
+ if (opts.wallet) pipeItems.push(v.check((value) => {
31
+ const type = toTypeInfo(value);
32
+ if (["ethereum", "eth"].includes(opts.wallet)) return isEqual(type, DID_TYPE_ETHEREUM);
33
+ if (["arcblock", "default"].includes(opts.wallet)) return isEqual(type, DID_TYPE_ARCBLOCK);
34
+ if (["passkey"].includes(opts.wallet)) return isEqual(type, DID_TYPE_PASSKEY);
35
+ return true;
36
+ }, `Expected wallet type to be "${opts.wallet}"`));
37
+ return v.pipe(...pipeItems);
38
+ }
39
+
40
+ //#endregion
41
+ export { vDID };
@@ -0,0 +1,15 @@
1
+ //#region src/v/helpers.d.ts
2
+ declare function vStripUnknown(): any;
3
+ declare function vValidate(schema: any, data: any): {
4
+ error?: any;
5
+ value?: any;
6
+ };
7
+ declare function vWhen(field: string, conditions: {
8
+ is: any;
9
+ then?: any;
10
+ otherwise?: any;
11
+ }): any;
12
+ declare function vForbidden(message?: string): any;
13
+ declare function vFilteredRecord(keySchema: any, valueSchema: any): any;
14
+ //#endregion
15
+ export { vFilteredRecord, vForbidden, vStripUnknown, vValidate, vWhen };
@@ -0,0 +1,61 @@
1
+ import * as v from "valibot";
2
+
3
+ //#region src/v/helpers.ts
4
+ function vStripUnknown() {
5
+ return v.transform((value) => value);
6
+ }
7
+ function vValidate(schema, data) {
8
+ const result = v.safeParse(schema, data);
9
+ if (result.success) return {
10
+ value: result.output,
11
+ error: void 0
12
+ };
13
+ return {
14
+ value: void 0,
15
+ error: {
16
+ message: result.issues.map((issue) => {
17
+ const path = issue.path?.map((p) => p.key).filter(Boolean).join(".");
18
+ return path ? `"${path}" ${issue.message}` : issue.message;
19
+ }).join("; "),
20
+ details: result.issues.map((issue) => ({
21
+ message: issue.message,
22
+ path: issue.path?.map((p) => p.key)
23
+ }))
24
+ }
25
+ };
26
+ }
27
+ function vWhen(field, conditions) {
28
+ return v.rawTransform(({ dataset, addIssue, NEVER }) => {
29
+ const obj = dataset.value;
30
+ if (!obj || typeof obj !== "object") return obj;
31
+ const targetSchema = (typeof conditions.is === "function" ? conditions.is(obj[field]) : obj[field] === conditions.is) ? conditions.then : conditions.otherwise;
32
+ if (!targetSchema) return obj;
33
+ const result = v.safeParse(targetSchema, obj);
34
+ if (!result.success) {
35
+ for (const issue of result.issues) addIssue({
36
+ message: issue.message,
37
+ path: issue.path
38
+ });
39
+ return NEVER;
40
+ }
41
+ return result.output;
42
+ });
43
+ }
44
+ function vForbidden(message = "Field is forbidden") {
45
+ return v.pipe(v.never(message));
46
+ }
47
+ function vFilteredRecord(keySchema, valueSchema) {
48
+ return v.pipe(v.record(v.string(), v.any()), v.transform((obj) => {
49
+ const result = {};
50
+ for (const [key, val] of Object.entries(obj)) {
51
+ if (!v.safeParse(keySchema, key).success) continue;
52
+ const valResult = v.safeParse(valueSchema, val);
53
+ if (!valResult.success) continue;
54
+ result[key] = valResult.output;
55
+ }
56
+ return result;
57
+ }));
58
+ }
59
+
60
+ //#endregion
61
+ export { vFilteredRecord, vForbidden, vStripUnknown, vValidate, vWhen };
@@ -0,0 +1,6 @@
1
+ import { VDIDOptions, vDID } from "./did.mjs";
2
+ import { vBN, vBNGreater, vBNGt, vBNGte, vBNLess, vBNLt, vBNLte, vBNMax, vBNMin, vBNNegative, vBNPositive } from "./bn.mjs";
3
+ import { vFilteredRecord, vForbidden, vStripUnknown, vValidate, vWhen } from "./helpers.mjs";
4
+ import { schemas } from "./schemas.mjs";
5
+ export * from "valibot";
6
+ export { type VDIDOptions, schemas, vBN, vBNGreater, vBNGt, vBNGte, vBNLess, vBNLt, vBNLte, vBNMax, vBNMin, vBNNegative, vBNPositive, vDID, vFilteredRecord, vForbidden, vStripUnknown, vValidate, vWhen };
@@ -0,0 +1,8 @@
1
+ import { vDID } from "./did.mjs";
2
+ import { vBN, vBNGreater, vBNGt, vBNGte, vBNLess, vBNLt, vBNLte, vBNMax, vBNMin, vBNNegative, vBNPositive } from "./bn.mjs";
3
+ import { vFilteredRecord, vForbidden, vStripUnknown, vValidate, vWhen } from "./helpers.mjs";
4
+ import { schemas } from "./schemas.mjs";
5
+
6
+ export * from "valibot"
7
+
8
+ export { schemas, vBN, vBNGreater, vBNGt, vBNGte, vBNLess, vBNLt, vBNLte, vBNMax, vBNMin, vBNNegative, vBNPositive, vDID, vFilteredRecord, vForbidden, vStripUnknown, vValidate, vWhen };