@arcblock/validator 1.27.15 → 1.28.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.
@@ -1,142 +1,150 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.DIDExtension = DIDExtension;
7
- const isEqual_1 = __importDefault(require("lodash/isEqual"));
8
- const mcrypto_1 = require("@ocap/mcrypto");
9
- const did_1 = require("@arcblock/did");
1
+ const require_rolldown_runtime = require('../_virtual/rolldown_runtime.js');
2
+ let lodash_isEqual = require("lodash/isEqual");
3
+ lodash_isEqual = require_rolldown_runtime.__toESM(lodash_isEqual);
4
+ let _ocap_mcrypto = require("@ocap/mcrypto");
5
+ let _arcblock_did = require("@arcblock/did");
6
+
7
+ //#region src/extension/did.ts
10
8
  const ruleTypes = {
11
- pk: Object.keys(mcrypto_1.types.KeyType),
12
- hash: Object.keys(mcrypto_1.types.HashType),
13
- role: Object.keys(mcrypto_1.types.RoleType),
9
+ pk: Object.keys(_ocap_mcrypto.types.KeyType),
10
+ hash: Object.keys(_ocap_mcrypto.types.HashType),
11
+ role: Object.keys(_ocap_mcrypto.types.RoleType)
14
12
  };
15
13
  function DIDExtension(root) {
16
- return {
17
- type: 'DID',
18
- base: root.string().trim(),
19
- messages: {
20
- 'did.empty': 'Expect {{#label}} to be non-empty string',
21
- 'did.invalid': 'Expect {{#label}} to be valid did',
22
- 'did.wallet': 'Expect wallet type of {{#label}} to be "{{#expected}}" wallet',
23
- 'did.pk': 'Expect pk type of {{#label}} to be "{{#expected}}"',
24
- 'did.hash': 'Expect hash type of {{#label}} to be "{{#expected}}"',
25
- 'did.role': 'Expect role type of {{#label}} to be "{{#expected}}"',
26
- 'did.prefix': 'Expect prefix of {{#label}} to be "{{#expected}}"',
27
- },
28
- validate(value, helpers) {
29
- if (!value || typeof value !== 'string') {
30
- return { errors: helpers.error('did.empty', { value }) };
31
- }
32
- if ((0, did_1.isValid)(value) === false) {
33
- return { errors: helpers.error('did.invalid', { value }) };
34
- }
35
- return { value };
36
- },
37
- rules: {
38
- type: {
39
- args: [
40
- {
41
- name: 'key',
42
- ref: true,
43
- assert: (v) => Object.keys(ruleTypes).includes(v),
44
- message: `must be one of ${Object.keys(ruleTypes).join(', ')}`,
45
- },
46
- {
47
- name: 'expected',
48
- // @ts-ignore
49
- assert: (v) => Object.keys(ruleTypes).some((x) => ruleTypes[x].includes(v)),
50
- message: 'must be valid type',
51
- },
52
- ],
53
- // The rule return structure is different from the root
54
- // eslint-disable-next-line consistent-return
55
- validate(value, helpers, args) {
56
- const typeStr = (0, did_1.toTypeInfoStr)(value);
57
- if (args.key === 'pk' && typeStr.pk !== args.expected) {
58
- return helpers.error('did.pk', { ...args, actual: typeStr.pk });
59
- }
60
- if (args.key === 'hash' && typeStr.hash !== args.expected) {
61
- return helpers.error('did.hash', { ...args, actual: typeStr.hash });
62
- }
63
- if (args.key === 'role' && typeStr.role !== args.expected) {
64
- return helpers.error('did.role', { ...args, actual: typeStr.role });
65
- }
66
- return value;
67
- },
68
- },
69
- pk: {
70
- method(type) {
71
- // @ts-ignore
72
- return this.$_addRule({ name: 'type', args: { key: 'pk', expected: type } });
73
- },
74
- },
75
- hash: {
76
- method(type) {
77
- // @ts-ignore
78
- return this.$_addRule({ name: 'type', args: { key: 'hash', expected: type } });
79
- },
80
- },
81
- role: {
82
- method(type) {
83
- // @ts-ignore
84
- return this.$_addRule({ name: 'type', args: { key: 'role', expected: type } });
85
- },
86
- },
87
- wallet: {
88
- method(type) {
89
- // @ts-ignore
90
- return this.$_addRule({ name: 'wallet', args: { type } });
91
- },
92
- args: [
93
- {
94
- name: 'type',
95
- ref: true,
96
- assert: (v) => ['arcblock', 'ethereum', 'default', 'eth', 'passkey'].includes(v),
97
- message: 'must be a string',
98
- normalize: (v) => v.trim(),
99
- },
100
- ],
101
- validate(value, helpers, args = {}) {
102
- const type = (0, did_1.toTypeInfo)(value);
103
- const typeStr = (0, did_1.toTypeInfoStr)(value);
104
- if (['ethereum', 'eth'].includes(args.type) && (0, isEqual_1.default)(type, did_1.DID_TYPE_ETHEREUM) === false) {
105
- return helpers.error('did.wallet', { expected: args.type, actual: JSON.stringify(typeStr) });
106
- }
107
- if (['arcblock', 'default'].includes(args.type) && (0, isEqual_1.default)(type, did_1.DID_TYPE_ARCBLOCK) === false) {
108
- return helpers.error('did.wallet', { expected: args.type, actual: JSON.stringify(typeStr) });
109
- }
110
- if (['passkey'].includes(args.type) && (0, isEqual_1.default)(type, did_1.DID_TYPE_PASSKEY) === false) {
111
- return helpers.error('did.wallet', { expected: args.type, actual: JSON.stringify(typeStr) });
112
- }
113
- return value;
114
- },
115
- },
116
- prefix: {
117
- method(str = '') {
118
- // @ts-ignore
119
- return this.$_addRule({ name: 'prefix', args: { str } });
120
- },
121
- args: [
122
- {
123
- name: 'str',
124
- ref: true,
125
- assert: (v) => ['', did_1.DID_PREFIX].includes(v),
126
- message: 'must be a string',
127
- normalize: (v) => v.trim(),
128
- },
129
- ],
130
- validate(value, helpers, args = {}) {
131
- if (args.str && value.startsWith(args.str) === false) {
132
- return helpers.error('did.prefix', { expected: args.str });
133
- }
134
- if (!args.str && (0, did_1.toAddress)(value) !== value) {
135
- return helpers.error('did.prefix', { expected: args.str });
136
- }
137
- return value;
138
- },
139
- },
140
- },
141
- };
14
+ return {
15
+ type: "DID",
16
+ base: root.string().trim(),
17
+ messages: {
18
+ "did.empty": "Expect {{#label}} to be non-empty string",
19
+ "did.invalid": "Expect {{#label}} to be valid did",
20
+ "did.wallet": "Expect wallet type of {{#label}} to be \"{{#expected}}\" wallet",
21
+ "did.pk": "Expect pk type of {{#label}} to be \"{{#expected}}\"",
22
+ "did.hash": "Expect hash type of {{#label}} to be \"{{#expected}}\"",
23
+ "did.role": "Expect role type of {{#label}} to be \"{{#expected}}\"",
24
+ "did.prefix": "Expect prefix of {{#label}} to be \"{{#expected}}\""
25
+ },
26
+ validate(value, helpers) {
27
+ if (!value || typeof value !== "string") return { errors: helpers.error("did.empty", { value }) };
28
+ if ((0, _arcblock_did.isValid)(value) === false) return { errors: helpers.error("did.invalid", { value }) };
29
+ return { value };
30
+ },
31
+ rules: {
32
+ type: {
33
+ args: [{
34
+ name: "key",
35
+ ref: true,
36
+ assert: (v) => Object.keys(ruleTypes).includes(v),
37
+ message: `must be one of ${Object.keys(ruleTypes).join(", ")}`
38
+ }, {
39
+ name: "expected",
40
+ assert: (v) => Object.keys(ruleTypes).some((x) => ruleTypes[x].includes(v)),
41
+ message: "must be valid type"
42
+ }],
43
+ validate(value, helpers, args) {
44
+ const typeStr = (0, _arcblock_did.toTypeInfoStr)(value);
45
+ if (args.key === "pk" && typeStr.pk !== args.expected) return helpers.error("did.pk", {
46
+ ...args,
47
+ actual: typeStr.pk
48
+ });
49
+ if (args.key === "hash" && typeStr.hash !== args.expected) return helpers.error("did.hash", {
50
+ ...args,
51
+ actual: typeStr.hash
52
+ });
53
+ if (args.key === "role" && typeStr.role !== args.expected) return helpers.error("did.role", {
54
+ ...args,
55
+ actual: typeStr.role
56
+ });
57
+ return value;
58
+ }
59
+ },
60
+ pk: { method(type) {
61
+ return this.$_addRule({
62
+ name: "type",
63
+ args: {
64
+ key: "pk",
65
+ expected: type
66
+ }
67
+ });
68
+ } },
69
+ hash: { method(type) {
70
+ return this.$_addRule({
71
+ name: "type",
72
+ args: {
73
+ key: "hash",
74
+ expected: type
75
+ }
76
+ });
77
+ } },
78
+ role: { method(type) {
79
+ return this.$_addRule({
80
+ name: "type",
81
+ args: {
82
+ key: "role",
83
+ expected: type
84
+ }
85
+ });
86
+ } },
87
+ wallet: {
88
+ method(type) {
89
+ return this.$_addRule({
90
+ name: "wallet",
91
+ args: { type }
92
+ });
93
+ },
94
+ args: [{
95
+ name: "type",
96
+ ref: true,
97
+ assert: (v) => [
98
+ "arcblock",
99
+ "ethereum",
100
+ "default",
101
+ "eth",
102
+ "passkey"
103
+ ].includes(v),
104
+ message: "must be a string",
105
+ normalize: (v) => v.trim()
106
+ }],
107
+ validate(value, helpers, args = {}) {
108
+ const type = (0, _arcblock_did.toTypeInfo)(value);
109
+ const typeStr = (0, _arcblock_did.toTypeInfoStr)(value);
110
+ if (["ethereum", "eth"].includes(args.type) && (0, lodash_isEqual.default)(type, _arcblock_did.DID_TYPE_ETHEREUM) === false) return helpers.error("did.wallet", {
111
+ expected: args.type,
112
+ actual: JSON.stringify(typeStr)
113
+ });
114
+ if (["arcblock", "default"].includes(args.type) && (0, lodash_isEqual.default)(type, _arcblock_did.DID_TYPE_ARCBLOCK) === false) return helpers.error("did.wallet", {
115
+ expected: args.type,
116
+ actual: JSON.stringify(typeStr)
117
+ });
118
+ if (["passkey"].includes(args.type) && (0, lodash_isEqual.default)(type, _arcblock_did.DID_TYPE_PASSKEY) === false) return helpers.error("did.wallet", {
119
+ expected: args.type,
120
+ actual: JSON.stringify(typeStr)
121
+ });
122
+ return value;
123
+ }
124
+ },
125
+ prefix: {
126
+ method(str = "") {
127
+ return this.$_addRule({
128
+ name: "prefix",
129
+ args: { str }
130
+ });
131
+ },
132
+ args: [{
133
+ name: "str",
134
+ ref: true,
135
+ assert: (v) => ["", _arcblock_did.DID_PREFIX].includes(v),
136
+ message: "must be a string",
137
+ normalize: (v) => v.trim()
138
+ }],
139
+ validate(value, helpers, args = {}) {
140
+ if (args.str && value.startsWith(args.str) === false) return helpers.error("did.prefix", { expected: args.str });
141
+ if (!args.str && (0, _arcblock_did.toAddress)(value) !== value) return helpers.error("did.prefix", { expected: args.str });
142
+ return value;
143
+ }
144
+ }
145
+ }
146
+ };
142
147
  }
148
+
149
+ //#endregion
150
+ exports.DIDExtension = DIDExtension;
package/lib/index.d.ts CHANGED
@@ -1,51 +1,55 @@
1
- import BaseJoi, { Root } from 'joi';
2
- import { BNSchema } from './extension/bn';
3
- import { DIDSchema } from './extension/did';
4
- export interface ExtendedRoot extends Root {
5
- DID(): DIDSchema;
6
- BN(): BNSchema;
1
+ import { BNSchema } from "./extension/bn.js";
2
+ import { DIDSchema } from "./extension/did.js";
3
+ import BaseJoi, { Root } from "joi";
4
+
5
+ //#region src/index.d.ts
6
+ interface ExtendedRoot extends Root {
7
+ DID(): DIDSchema;
8
+ BN(): BNSchema;
7
9
  }
8
- export declare const Joi: ExtendedRoot;
9
- export declare const schemas: {
10
- context: BaseJoi.ObjectSchema<any>;
11
- tokenInput: BaseJoi.ObjectSchema<any>;
12
- multiInput: BaseJoi.ArraySchema<any[]>;
13
- multiSig: BaseJoi.ArraySchema<any[]>;
14
- foreignToken: BaseJoi.ObjectSchema<any>;
15
- variableInput: BaseJoi.ObjectSchema<any>;
16
- nftDisplay: BaseJoi.ObjectSchema<any>;
17
- nftEndpoint: BaseJoi.ObjectSchema<any>;
18
- nftIssuer: BaseJoi.ObjectSchema<any>;
19
- tokenHolder: BaseJoi.AlternativesSchema<any>;
20
- assetProps: {
21
- address: DIDSchema;
22
- moniker: BaseJoi.StringSchema<string>;
23
- data: BaseJoi.AnySchema<any>;
24
- readonly: BaseJoi.BooleanSchema<boolean>;
25
- transferrable: BaseJoi.BooleanSchema<boolean>;
26
- ttl: BaseJoi.NumberSchema<number>;
27
- parent: DIDSchema;
28
- issuer: DIDSchema;
29
- endpoint: BaseJoi.ObjectSchema<any>;
30
- display: BaseJoi.ObjectSchema<any>;
31
- tags: BaseJoi.ArraySchema<any[]>;
32
- };
33
- assetSchema: BaseJoi.ObjectSchema<any>;
34
- factoryProps: {
35
- address: DIDSchema;
36
- name: BaseJoi.StringSchema<string>;
37
- description: BaseJoi.StringSchema<string>;
38
- settlement: BaseJoi.StringSchema<string>;
39
- limit: BaseJoi.NumberSchema<number>;
40
- trustedIssuers: BaseJoi.ArraySchema<any[]>;
41
- data: BaseJoi.AnySchema<any>;
42
- display: BaseJoi.ObjectSchema<any>;
43
- input: BaseJoi.ObjectSchema<any>;
44
- output: BaseJoi.ObjectSchema<any>;
45
- hooks: BaseJoi.ArraySchema<any[]>;
46
- };
47
- factorySchema: BaseJoi.ObjectSchema<any>;
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>;
48
50
  };
49
- export declare const patterns: {
50
- txHash: RegExp;
51
+ declare const patterns: {
52
+ txHash: RegExp;
51
53
  };
54
+ //#endregion
55
+ export { ExtendedRoot, Joi, patterns, schemas };