@ocap/state 1.14.3 → 1.14.4

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.
Files changed (2) hide show
  1. package/lib/states/asset.js +72 -11
  2. package/package.json +8 -8
@@ -1,7 +1,40 @@
1
1
  const pick = require('lodash/pick');
2
+ const Joi = require('@ocap/validator');
2
3
 
3
4
  const { create: createStateContext, update: updateStateContext } = require('../contexts/state');
4
5
 
6
+ const props = {
7
+ address: Joi.DID().role('ROLE_ASSET').required(),
8
+ moniker: Joi.string().min(2).max(255).required(),
9
+ data: Joi.any().required(),
10
+ readonly: Joi.boolean().default(false),
11
+ transferrable: Joi.boolean().default(false),
12
+ ttl: Joi.number().min(0).default(0),
13
+ parent: Joi.DID().optional().allow(''),
14
+ issuer: Joi.DID().optional().allow(''),
15
+ endpoint: Joi.object({
16
+ id: Joi.string().uri({ scheme: ['http', 'https'] }).required(), // prettier-ignore
17
+ scope: Joi.string().valid('public', 'private').default('public'),
18
+ }).optional(),
19
+ display: Joi.object({
20
+ type: Joi.string().valid('svg', 'url', 'uri').required(),
21
+ content: Joi.string()
22
+ .when('type', { is: 'uri', then: Joi.string().dataUri().required() })
23
+ .when('type', { is: 'url', then: Joi.string().uri({ scheme: ['http', 'https'] }).required() }), // prettier-ignore
24
+ }).optional(),
25
+ tags: Joi.array().items(Joi.string().min(1)).optional(),
26
+ };
27
+
28
+ const schema = Joi.object({
29
+ ...props,
30
+ owner: Joi.DID().required(),
31
+ consumedTime: Joi.date().iso().raw().allow(''),
32
+ context: Joi.schemas.context,
33
+ }).options({
34
+ stripUnknown: true,
35
+ noDefaults: false,
36
+ });
37
+
5
38
  const create = (attrs, context) => {
6
39
  const asset = {
7
40
  consumedTime: '',
@@ -10,16 +43,25 @@ const create = (attrs, context) => {
10
43
  readonly: false,
11
44
  transferrable: true,
12
45
  ttl: 0, // means unlimited
13
- stake: null,
46
+ tags: [],
14
47
  context: createStateContext(context),
15
- ...pick(attrs, ['address', 'owner', 'issuer', 'moniker', 'parent', 'data', 'readonly', 'transferrable', 'ttl']),
48
+ ...pick(attrs, [
49
+ 'address',
50
+ 'owner',
51
+ 'issuer',
52
+ 'moniker',
53
+ 'parent',
54
+ 'data',
55
+ 'readonly',
56
+ 'transferrable',
57
+ 'ttl',
58
+ 'endpoint',
59
+ 'display',
60
+ 'tags',
61
+ ]),
16
62
  };
17
63
 
18
- if (!asset.data) {
19
- asset.data = null;
20
- }
21
-
22
- return asset;
64
+ return validate(asset);
23
65
  };
24
66
 
25
67
  const update = (state, attrs, context) => {
@@ -29,11 +71,30 @@ const update = (state, attrs, context) => {
29
71
  context: updateStateContext(state.context, context),
30
72
  };
31
73
 
32
- if (!asset.data) {
33
- asset.data = null;
74
+ return validate(asset);
75
+ };
76
+
77
+ const validate = (state) => {
78
+ const { value, error } = schema.validate(state);
79
+ if (error) {
80
+ throw new Error(`Invalid asset: ${error.details.map((x) => x.message).join(', ')}`);
34
81
  }
35
82
 
36
- return asset;
83
+ ['endpoint', 'display'].forEach((key) => {
84
+ if (!value[key]) {
85
+ delete value[key];
86
+ }
87
+ });
88
+
89
+ return value;
37
90
  };
38
91
 
39
- module.exports = { create, update };
92
+ module.exports = {
93
+ create,
94
+ update,
95
+ validate,
96
+ schema: Joi.object(props).options({
97
+ stripUnknown: true,
98
+ noDefaults: false,
99
+ }),
100
+ };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.14.3",
6
+ "version": "1.14.4",
7
7
  "description": "",
8
8
  "main": "lib/index.js",
9
9
  "files": [
@@ -16,12 +16,12 @@
16
16
  "coverage": "npm run test -- --coverage"
17
17
  },
18
18
  "dependencies": {
19
- "@arcblock/did": "1.14.3",
20
- "@ocap/contract": "1.14.3",
21
- "@ocap/mcrypto": "1.14.3",
22
- "@ocap/message": "1.14.3",
23
- "@ocap/util": "1.14.3",
24
- "@ocap/validator": "1.14.3",
19
+ "@arcblock/did": "1.14.4",
20
+ "@ocap/contract": "1.14.4",
21
+ "@ocap/mcrypto": "1.14.4",
22
+ "@ocap/message": "1.14.4",
23
+ "@ocap/util": "1.14.4",
24
+ "@ocap/validator": "1.14.4",
25
25
  "bloom-filters": "^1.3.1",
26
26
  "lodash": "^4.17.21"
27
27
  },
@@ -31,5 +31,5 @@
31
31
  "keywords": [],
32
32
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
33
33
  "license": "MIT",
34
- "gitHead": "05775365b339c77fa517d1a3180d347fee66d146"
34
+ "gitHead": "33f7b1d72aa51e58a269b2681bd19e2119d1721e"
35
35
  }