@ocap/state 1.17.11 → 1.17.14

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.
@@ -4,30 +4,8 @@ const { CustomError: Error } = require('@ocap/util/lib/error');
4
4
 
5
5
  const { create: createStateContext, update: updateStateContext } = require('../contexts/state');
6
6
 
7
- const props = {
8
- address: Joi.DID().role('ROLE_ASSET').required(),
9
- moniker: Joi.string().min(2).max(255).required(),
10
- data: Joi.any().required(),
11
- readonly: Joi.boolean().default(false),
12
- transferrable: Joi.boolean().default(false),
13
- ttl: Joi.number().min(0).default(0),
14
- parent: Joi.DID().optional().allow(''),
15
- issuer: Joi.DID().optional().allow(''),
16
- endpoint: Joi.object({
17
- id: Joi.string().uri({ scheme: ['http', 'https'] }).required(), // prettier-ignore
18
- scope: Joi.string().valid('public', 'private').default('public'),
19
- }).optional(),
20
- display: Joi.object({
21
- type: Joi.string().valid('svg', 'url', 'uri').required(),
22
- content: Joi.string()
23
- .when('type', { is: 'uri', then: Joi.string().dataUri().required() })
24
- .when('type', { is: 'url', then: Joi.string().uri({ scheme: ['http', 'https'] }).required() }), // prettier-ignore
25
- }).optional(),
26
- tags: Joi.array().items(Joi.string().min(1)).optional(),
27
- };
28
-
29
7
  const schema = Joi.object({
30
- ...props,
8
+ ...schemas.assetProps,
31
9
  owner: Joi.DID().required(),
32
10
  consumedTime: Joi.date().iso().raw().allow(''),
33
11
  context: schemas.context,
@@ -98,8 +76,5 @@ module.exports = {
98
76
  create,
99
77
  update,
100
78
  validate,
101
- schema: Joi.object(props).options({
102
- stripUnknown: true,
103
- noDefaults: false,
104
- }),
79
+ schema: schemas.assetSchema,
105
80
  };
@@ -1,8 +1,21 @@
1
1
  const pick = require('lodash/pick');
2
+ const { Joi, schemas } = require('@arcblock/validator');
2
3
  const { compile, merge, getQuota } = require('@ocap/contract');
3
4
 
4
5
  const { create: createStateContext, update: updateStateContext } = require('../contexts/state');
5
6
 
7
+ const stateSchema = Joi.object({
8
+ ...schemas.factoryProps,
9
+ owner: Joi.DID().required(),
10
+ numMinted: Joi.number().min(0).default(0),
11
+ lastSettlement: Joi.date().iso().raw().allow(''),
12
+ tokens: Joi.object().pattern(Joi.DID().role('ROLE_TOKEN'), Joi.BN().min(0)).default({}),
13
+ context: schemas.context,
14
+ }).options({
15
+ stripUnknown: true,
16
+ noDefaults: false,
17
+ });
18
+
6
19
  const compileHook = (hook, quota) => {
7
20
  if (hook.type === 'contract') {
8
21
  hook.compiled = merge(compile(hook.hook, quota));
@@ -13,18 +26,9 @@ const compileHook = (hook, quota) => {
13
26
 
14
27
  const create = (attrs, context) => {
15
28
  const factory = {
16
- address: '',
17
- owner: '',
18
- name: '',
19
- description: '',
20
- settlement: 'instant',
21
- limit: 0,
22
29
  numMinted: 0,
23
30
  lastSettlement: '',
24
- balance: '0',
25
31
  tokens: {},
26
- trustedIssuers: [],
27
- stake: null,
28
32
  context: createStateContext(context),
29
33
  ...pick(attrs, [
30
34
  'address',
@@ -50,13 +54,29 @@ const create = (attrs, context) => {
50
54
  const quota = getQuota(factory.input);
51
55
  factory.hooks = (factory.hooks || []).map((x) => compileHook(x, quota));
52
56
 
53
- return factory;
57
+ return validate(factory);
54
58
  };
55
59
 
56
- const update = (state, attrs, context) => ({
57
- ...state,
58
- ...pick(attrs, ['numMinted', 'tokens']),
59
- context: updateStateContext(state.context, context),
60
- });
60
+ const update = (state, attrs, context) =>
61
+ validate({
62
+ ...state,
63
+ ...pick(attrs, ['numMinted', 'tokens']),
64
+ context: updateStateContext(state.context, context),
65
+ });
66
+
67
+ const validate = (state) => {
68
+ const { value, error } = stateSchema.validate(state);
69
+ if (error) {
70
+ throw new Error(`Invalid factory: ${error.details.map((x) => x.message).join(', ')}`);
71
+ }
72
+
73
+ ['display'].forEach((key) => {
74
+ if (!value[key]) {
75
+ delete value[key];
76
+ }
77
+ });
78
+
79
+ return value;
80
+ };
61
81
 
62
- module.exports = { create, update };
82
+ module.exports = { create, update, validate };
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.17.11",
6
+ "version": "1.17.14",
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.17.11",
20
- "@arcblock/validator": "1.17.11",
21
- "@ocap/contract": "1.17.11",
22
- "@ocap/mcrypto": "1.17.11",
23
- "@ocap/message": "1.17.11",
24
- "@ocap/util": "1.17.11",
19
+ "@arcblock/did": "1.17.14",
20
+ "@arcblock/validator": "1.17.14",
21
+ "@ocap/contract": "1.17.14",
22
+ "@ocap/mcrypto": "1.17.14",
23
+ "@ocap/message": "1.17.14",
24
+ "@ocap/util": "1.17.14",
25
25
  "bloom-filters": "^1.3.9",
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": "32ee93b1f1026709bb1b00a58b4b3dc5d551f94a"
34
+ "gitHead": "3e0c14fb2ca3f56639599aec7929c3de54ee61c7"
35
35
  }