@toa.io/core 1.0.0-alpha.182 → 1.0.0-alpha.190

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@toa.io/core",
3
- "version": "1.0.0-alpha.182",
3
+ "version": "1.0.0-alpha.190",
4
4
  "description": "Toa Core",
5
5
  "author": "temich <tema.gurtovoy@gmail.com>",
6
6
  "homepage": "https://github.com/toa-io/toa#readme",
@@ -26,5 +26,5 @@
26
26
  "error-value": "0.3.0",
27
27
  "openspan": "1.0.0-alpha.173"
28
28
  },
29
- "gitHead": "99d0b79680455e8746274bcfa07a5674e8514bff"
29
+ "gitHead": "494b1c8f05c21f377f1152a9d89df5cbbd1001de"
30
30
  }
@@ -1,16 +1,18 @@
1
1
  'use strict'
2
2
 
3
3
  const { difference, newid } = require('@toa.io/generic')
4
- const { EntityContractException } = require('../exceptions')
4
+ const { EntityContractException, EntityGuardException } = require('../exceptions')
5
5
 
6
6
  class Entity {
7
7
  deleted = false
8
8
  #schema
9
+ #guards
9
10
  #origin = null
10
11
  #state
11
12
 
12
- constructor (schema, argument) {
13
+ constructor (schema, argument, guards) {
13
14
  this.#schema = schema
15
+ this.#guards = guards
14
16
 
15
17
  if (typeof argument === 'object') {
16
18
  const object = structuredClone(argument)
@@ -32,6 +34,9 @@ class Entity {
32
34
  if (error !== null)
33
35
  throw new EntityContractException(error, value)
34
36
 
37
+ if (!optional)
38
+ this.#guard(value)
39
+
35
40
  this.#set(value)
36
41
  }
37
42
 
@@ -51,6 +56,18 @@ class Entity {
51
56
  this.set(value, true)
52
57
  }
53
58
 
59
+ #guard (value) {
60
+ if (this.#guards === undefined)
61
+ return
62
+
63
+ for (const guard of this.#guards) {
64
+ const ok = guard.fit(value)
65
+
66
+ if (ok === false)
67
+ throw new EntityGuardException(guard.name, value)
68
+ }
69
+ }
70
+
54
71
  #set (value) {
55
72
  if (!('_trailers' in value))
56
73
  Object.defineProperty(value, '_trailers', {
@@ -8,9 +8,11 @@ const { Changeset } = require('./changeset')
8
8
 
9
9
  class Factory {
10
10
  #schema
11
+ #guards
11
12
 
12
- constructor (schema) {
13
+ constructor (schema, guards) {
13
14
  this.#schema = schema
15
+ this.#guards = guards
14
16
  }
15
17
 
16
18
  fit (values) {
@@ -18,11 +20,11 @@ class Factory {
18
20
  }
19
21
 
20
22
  init (id) {
21
- return new Entity(this.#schema, id)
23
+ return new Entity(this.#schema, id, this.#guards)
22
24
  }
23
25
 
24
26
  object (record) {
25
- return new Entity(this.#schema, record)
27
+ return new Entity(this.#schema, record, this.#guards)
26
28
  }
27
29
 
28
30
  objects (recordset) {
package/src/exceptions.js CHANGED
@@ -11,6 +11,7 @@ const codes = {
11
11
  RequestConflict: 203,
12
12
  ResponseContract: 211,
13
13
  EntityContract: 212,
14
+ EntityGuard: 213,
14
15
  QuerySyntax: 221,
15
16
 
16
17
  State: 300,
@@ -73,12 +74,17 @@ class EntityContractException extends ContractException {
73
74
  constructor (error, cause) { super(codes.EntityContract, error, cause) }
74
75
  }
75
76
 
77
+ class EntityGuardException extends ContractException {
78
+ constructor (name, cause) { super(codes.EntityGuard, name, cause) }
79
+ }
80
+
76
81
  // #region exports
77
82
  exports.Exception = Exception
78
83
  exports.SystemException = SystemException
79
84
  exports.RequestContractException = RequestContractException
80
85
  exports.ResponseContractException = ResponseContractException
81
86
  exports.EntityContractException = EntityContractException
87
+ exports.EntityGuardException = EntityGuardException
82
88
 
83
89
  for (const [name, code] of Object.entries(codes)) {
84
90
  const classname = name + 'Exception'
package/src/guard.js ADDED
@@ -0,0 +1,17 @@
1
+ 'use strict'
2
+
3
+ class Guard {
4
+ name
5
+ #bridge
6
+
7
+ constructor (name, bridge) {
8
+ this.name = name
9
+ this.#bridge = bridge
10
+ }
11
+
12
+ fit (state) {
13
+ return this.#bridge.fit(state)
14
+ }
15
+ }
16
+
17
+ exports.Guard = Guard
package/src/index.js CHANGED
@@ -21,6 +21,7 @@ const { State } = require('./state')
21
21
  const { Transition } = require('./transition')
22
22
  const { Transmission } = require('./transmission')
23
23
  const { Unmanaged } = require('./unmanaged')
24
+ const { Guard } = require('./guard')
24
25
 
25
26
  exports.entities = require('./entities')
26
27
  exports.exceptions = require('./exceptions')
@@ -49,3 +50,4 @@ exports.State = State
49
50
  exports.Transition = Transition
50
51
  exports.Transmission = Transmission
51
52
  exports.Unmanaged = Unmanaged
53
+ exports.Guard = Guard
package/src/transition.js CHANGED
@@ -24,7 +24,7 @@ class Transition extends Operation {
24
24
 
25
25
  if (store.scope === null || store.scope.deleted === true)
26
26
  throw new StateNotFoundException()
27
-
27
+
28
28
  store.state = store.scope.get()
29
29
  }
30
30