@hello.nrfcloud.com/proto-map 12.1.0 → 12.1.2

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/api/LwM2M.ts CHANGED
@@ -20,11 +20,18 @@ export const ObjectInstanceID = Type.Integer({
20
20
  examples: [0, 1],
21
21
  })
22
22
 
23
+ const String = Type.String({ minLength: 1, title: 'LwM2M string value' })
24
+ const Number = Type.Number({ title: 'LwM2M number value' })
25
+ const Boolean = Type.Boolean({ title: 'LwM2M boolean value' })
26
+
23
27
  export const Resources = Type.Record(
24
28
  Type.Integer({ minimum: 0 }),
25
29
  Type.Union([
26
- Type.String({ minLength: 1, title: 'LwM2M string value' }),
27
- Type.Number({ title: 'LwM2M number value' }),
28
- Type.Boolean({ title: 'LwM2M boolean value' }),
30
+ String,
31
+ Type.Array(String),
32
+ Number,
33
+ Type.Array(Number),
34
+ Boolean,
35
+ Type.Array(Boolean),
29
36
  ]),
30
37
  )
package/dist/api/LwM2M.js CHANGED
@@ -21,17 +21,23 @@ export var ObjectInstanceID = Type.Integer({
21
21
  1
22
22
  ]
23
23
  });
24
+ var String = Type.String({
25
+ minLength: 1,
26
+ title: 'LwM2M string value'
27
+ });
28
+ var Number = Type.Number({
29
+ title: 'LwM2M number value'
30
+ });
31
+ var Boolean = Type.Boolean({
32
+ title: 'LwM2M boolean value'
33
+ });
24
34
  export var Resources = Type.Record(Type.Integer({
25
35
  minimum: 0
26
36
  }), Type.Union([
27
- Type.String({
28
- minLength: 1,
29
- title: 'LwM2M string value'
30
- }),
31
- Type.Number({
32
- title: 'LwM2M number value'
33
- }),
34
- Type.Boolean({
35
- title: 'LwM2M boolean value'
36
- })
37
+ String,
38
+ Type.Array(String),
39
+ Number,
40
+ Type.Array(Number),
41
+ Boolean,
42
+ Type.Array(Boolean)
37
43
  ]));
@@ -104,6 +104,11 @@ export var generateValidator = function(param) {
104
104
  };
105
105
  var toResourceValidator = function(Resource) {
106
106
  var valueValidator = ts.factory.createIdentifier(typeToValidator(Resource.Type));
107
+ if (Resource.MultipleInstances === 'Multiple') {
108
+ valueValidator = ts.factory.createCallExpression(ts.factory.createIdentifier('MultipleInstanceResource'), undefined, [
109
+ valueValidator
110
+ ]);
111
+ }
107
112
  if (Resource.Mandatory === 'Optional') return ts.factory.createCallExpression(ts.factory.createIdentifier('OptionalResource'), undefined, [
108
113
  valueValidator
109
114
  ]);
@@ -130,10 +135,11 @@ var getResourceValidators = function(param) {
130
135
  return new Set((Array.isArray(Resources.Item) ? Resources.Item : [
131
136
  Resources.Item
132
137
  ]).map(function(param) {
133
- var Type = param.Type, Mandatory = param.Mandatory;
138
+ var Type = param.Type, Mandatory = param.Mandatory, MultipleInstances = param.MultipleInstances;
134
139
  return [
135
140
  typeToValidator(Type),
136
- Mandatory === 'Optional' ? 'OptionalResource' : undefined
141
+ Mandatory === 'Optional' ? 'OptionalResource' : undefined,
142
+ MultipleInstances === 'Multiple' ? 'MultipleInstanceResource' : undefined
137
143
  ];
138
144
  }).flat().filter(function(d) {
139
145
  return d !== undefined;
@@ -1,4 +1,4 @@
1
- import { TimeResource, StringResource, OptionalResource, validateInstance } from "../validation.js";
1
+ import { TimeResource, StringResource, OptionalResource, MultipleInstanceResource, validateInstance } from "../validation.js";
2
2
  import { LwM2MObjectID } from "../LwM2MObjectID.js";
3
3
  /**
4
4
  * Validate nRF Cloud Service Info (14401)
@@ -7,6 +7,6 @@ import { LwM2MObjectID } from "../LwM2MObjectID.js";
7
7
  */ export var validate14401 = function(o) {
8
8
  return validateInstance(LwM2MObjectID.NRFCloudServiceInfo_14401, "1.0", {
9
9
  99: TimeResource,
10
- 0: OptionalResource(StringResource)
10
+ 0: OptionalResource(MultipleInstanceResource(StringResource))
11
11
  })(o);
12
12
  };
@@ -1,13 +1,12 @@
1
1
  import { describe, it } from 'node:test';
2
2
  import { validate } from './validate.js';
3
3
  import { validators } from './validators.js';
4
- import { LwM2MObjectID } from './LwM2MObjectID.js';
5
4
  import assert from 'node:assert';
6
5
  void describe('validate()', function() {
7
6
  var v = validate(validators);
8
7
  void it('should validate a LwM2M object instance', function() {
9
8
  var object = {
10
- ObjectID: LwM2MObjectID.Geolocation_14201,
9
+ ObjectID: 14201,
11
10
  ObjectVersion: '1.0',
12
11
  Resources: {
13
12
  '0': 62.469414,
@@ -42,4 +41,20 @@ void describe('validate()', function() {
42
41
  var maybeValid = v(object);
43
42
  assert.deepEqual('object' in maybeValid && maybeValid.object, object);
44
43
  });
44
+ void it('should validate an object with multiple instance resource', function() {
45
+ var object = {
46
+ ObjectID: 14401,
47
+ ObjectVersion: '1.0',
48
+ Resources: {
49
+ '0': [
50
+ 'BOOT',
51
+ 'MODEM',
52
+ 'APP'
53
+ ],
54
+ '99': 1717409966000
55
+ }
56
+ };
57
+ var maybeValid = v(object);
58
+ assert.deepEqual('object' in maybeValid && maybeValid.object, object);
59
+ });
45
60
  });
@@ -80,10 +80,10 @@ export var isLwM2MObject = function(object) {
80
80
  // All values must be number, string, boolean
81
81
  for(var _iterator = Object.values(object.Resources)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
82
82
  var v = _step.value;
83
- if (v === undefined) continue;
84
- if (typeof v === 'string') continue;
85
- if (typeof v === 'boolean') continue;
86
- if (typeof v === 'number') continue;
83
+ if (isSimpleResource(v)) continue;
84
+ if (Array.isArray(v) && v.every(function(v) {
85
+ return isSimpleResource(v);
86
+ })) continue;
87
87
  return error("Invalid value type ".concat(typeof v === "undefined" ? "undefined" : _type_of(v)));
88
88
  }
89
89
  } catch (err) {
@@ -104,6 +104,13 @@ export var isLwM2MObject = function(object) {
104
104
  object: object
105
105
  };
106
106
  };
107
+ var isSimpleResource = function(v) {
108
+ if (v === undefined) return true;
109
+ if (typeof v === 'string') return true;
110
+ if (typeof v === 'boolean') return true;
111
+ if (typeof v === 'number') return true;
112
+ return false;
113
+ };
107
114
  export var validateInstance = function(ObjectID, ObjectVersion, Resources) {
108
115
  return function(o) {
109
116
  var error = function(message) {
@@ -165,3 +172,8 @@ export var OptionalResource = function(validator) {
165
172
  return r === undefined ? true : validator(r);
166
173
  };
167
174
  };
175
+ export var MultipleInstanceResource = function(validator) {
176
+ return function(r) {
177
+ return Array.isArray(r) && r.every(validator);
178
+ };
179
+ };
@@ -0,0 +1,39 @@
1
+ import assert from 'node:assert';
2
+ import { describe, it } from 'node:test';
3
+ import { MultipleInstanceResource, OptionalResource, StringResource } from './validation.js';
4
+ void describe('MultipleInstanceResource()', function() {
5
+ void it('should validate a multiple instance resource', function() {
6
+ assert.equal(true, MultipleInstanceResource(StringResource)([
7
+ 'BOOT',
8
+ 'MODEM',
9
+ 'APP'
10
+ ]));
11
+ });
12
+ });
13
+ void describe('StringResource()', function() {
14
+ void it('should validate a string', function() {
15
+ assert.equal(true, StringResource('test'));
16
+ });
17
+ });
18
+ void describe('OptionalResource()', function() {
19
+ void it('should validate an undefined resource', function() {
20
+ assert.equal(true, OptionalResource(StringResource)(undefined));
21
+ });
22
+ void it('should validate an defined resource', function() {
23
+ assert.equal(true, OptionalResource(StringResource)('foo'));
24
+ });
25
+ void it('should not validate an invalid defined resource', function() {
26
+ assert.equal(false, OptionalResource(StringResource)(true));
27
+ });
28
+ });
29
+ void describe('combination', function() {
30
+ void it('should validate OptionalResource(MultipleInstanceResource(StringResource))', function() {
31
+ assert.equal(true, OptionalResource(MultipleInstanceResource(StringResource))([
32
+ 'BOOT',
33
+ 'MODEM',
34
+ 'APP'
35
+ ]));
36
+ assert.equal(true, OptionalResource(MultipleInstanceResource(StringResource))(undefined));
37
+ assert.equal(false, OptionalResource(MultipleInstanceResource(StringResource))('foo'));
38
+ });
39
+ });
@@ -1,5 +1,5 @@
1
1
  import type { LwM2MObjectInstance } from "../LwM2MObjectInstance.js";
2
- import { TimeResource, StringResource, OptionalResource, validateInstance } from "../validation.js";
2
+ import { TimeResource, StringResource, OptionalResource, MultipleInstanceResource, validateInstance } from "../validation.js";
3
3
  import type { NRFCloudServiceInfo_14401 } from "../objects.js";
4
4
  import { LwM2MObjectID } from "../LwM2MObjectID.js";
5
5
  /**
@@ -11,4 +11,4 @@ export const validate14401 = (o: unknown): {
11
11
  error: Error;
12
12
  } | {
13
13
  object: LwM2MObjectInstance<NRFCloudServiceInfo_14401>;
14
- } => validateInstance<NRFCloudServiceInfo_14401>(LwM2MObjectID.NRFCloudServiceInfo_14401, "1.0", { 99: TimeResource, 0: OptionalResource(StringResource) })(o);
14
+ } => validateInstance<NRFCloudServiceInfo_14401>(LwM2MObjectID.NRFCloudServiceInfo_14401, "1.0", { 99: TimeResource, 0: OptionalResource(MultipleInstanceResource(StringResource)) })(o);
@@ -1,14 +1,13 @@
1
1
  import { describe, it } from 'node:test'
2
2
  import { validate } from './validate.js'
3
3
  import { validators } from './validators.js'
4
- import { LwM2MObjectID } from './LwM2MObjectID.js'
5
4
  import assert from 'node:assert'
6
5
 
7
6
  void describe('validate()', () => {
8
7
  const v = validate(validators)
9
8
  void it('should validate a LwM2M object instance', () => {
10
9
  const object = {
11
- ObjectID: LwM2MObjectID.Geolocation_14201,
10
+ ObjectID: 14201,
12
11
  ObjectVersion: '1.0',
13
12
  Resources: {
14
13
  '0': 62.469414,
@@ -45,4 +44,17 @@ void describe('validate()', () => {
45
44
  const maybeValid = v(object)
46
45
  assert.deepEqual('object' in maybeValid && maybeValid.object, object)
47
46
  })
47
+
48
+ void it('should validate an object with multiple instance resource', () => {
49
+ const object = {
50
+ ObjectID: 14401,
51
+ ObjectVersion: '1.0',
52
+ Resources: {
53
+ '0': ['BOOT', 'MODEM', 'APP'],
54
+ '99': 1717409966000,
55
+ },
56
+ }
57
+ const maybeValid = v(object)
58
+ assert.deepEqual('object' in maybeValid && maybeValid.object, object)
59
+ })
48
60
  })
@@ -0,0 +1,55 @@
1
+ import assert from 'node:assert'
2
+ import { describe, it } from 'node:test'
3
+ import {
4
+ MultipleInstanceResource,
5
+ OptionalResource,
6
+ StringResource,
7
+ } from './validation.js'
8
+
9
+ void describe('MultipleInstanceResource()', () => {
10
+ void it('should validate a multiple instance resource', () => {
11
+ assert.equal(
12
+ true,
13
+ MultipleInstanceResource(StringResource)(['BOOT', 'MODEM', 'APP']),
14
+ )
15
+ })
16
+ })
17
+
18
+ void describe('StringResource()', () => {
19
+ void it('should validate a string', () => {
20
+ assert.equal(true, StringResource('test'))
21
+ })
22
+ })
23
+
24
+ void describe('OptionalResource()', () => {
25
+ void it('should validate an undefined resource', () => {
26
+ assert.equal(true, OptionalResource(StringResource)(undefined))
27
+ })
28
+ void it('should validate an defined resource', () => {
29
+ assert.equal(true, OptionalResource(StringResource)('foo'))
30
+ })
31
+ void it('should not validate an invalid defined resource', () => {
32
+ assert.equal(false, OptionalResource(StringResource)(true))
33
+ })
34
+ })
35
+
36
+ void describe('combination', () => {
37
+ void it('should validate OptionalResource(MultipleInstanceResource(StringResource))', () => {
38
+ assert.equal(
39
+ true,
40
+ OptionalResource(MultipleInstanceResource(StringResource))([
41
+ 'BOOT',
42
+ 'MODEM',
43
+ 'APP',
44
+ ]),
45
+ )
46
+ assert.equal(
47
+ true,
48
+ OptionalResource(MultipleInstanceResource(StringResource))(undefined),
49
+ )
50
+ assert.equal(
51
+ false,
52
+ OptionalResource(MultipleInstanceResource(StringResource))('foo'),
53
+ )
54
+ })
55
+ })
@@ -50,15 +50,23 @@ export const isLwM2MObject = (
50
50
  return error(`All resource IDs must be a number`)
51
51
  // All values must be number, string, boolean
52
52
  for (const v of Object.values(object.Resources)) {
53
- if (v === undefined) continue
54
- if (typeof v === 'string') continue
55
- if (typeof v === 'boolean') continue
56
- if (typeof v === 'number') continue
53
+ if (isSimpleResource(v)) continue
54
+ if (Array.isArray(v) && v.every((v) => isSimpleResource(v))) continue
57
55
  return error(`Invalid value type ${typeof v}`)
58
56
  }
59
57
  return { object: object as LwM2MObjectInstance }
60
58
  }
61
59
 
60
+ const isSimpleResource = (
61
+ v?: unknown,
62
+ ): v is number | string | boolean | undefined => {
63
+ if (v === undefined) return true
64
+ if (typeof v === 'string') return true
65
+ if (typeof v === 'boolean') return true
66
+ if (typeof v === 'number') return true
67
+ return false
68
+ }
69
+
62
70
  export const validateInstance =
63
71
  <O extends LwM2MObjectInstance>(
64
72
  ObjectID: LwM2MObjectID,
@@ -99,12 +107,11 @@ export const BooleanResource = (r: unknown): r is boolean =>
99
107
  typeof r === 'boolean'
100
108
 
101
109
  export const OptionalResource =
102
- (
103
- validator:
104
- | typeof NumberResource
105
- | typeof TimeResource
106
- | typeof StringResource
107
- | typeof BooleanResource,
108
- ) =>
110
+ (validator: (r: unknown) => boolean) =>
109
111
  (r: unknown): boolean =>
110
112
  r === undefined ? true : validator(r)
113
+
114
+ export const MultipleInstanceResource =
115
+ (validator: (r: unknown) => boolean) =>
116
+ (r: unknown): boolean =>
117
+ Array.isArray(r) && r.every(validator)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hello.nrfcloud.com/proto-map",
3
- "version": "12.1.0",
3
+ "version": "12.1.2",
4
4
  "description": "Documents the communication protocol between devices, the hello.nrfcloud.com/map backend and web application",
5
5
  "type": "module",
6
6
  "exports": {
@@ -39,7 +39,7 @@
39
39
  "@bifravst/prettier-config": "1.0.2",
40
40
  "@commitlint/config-conventional": "19.2.2",
41
41
  "@swc/cli": "0.3.12",
42
- "@swc/core": "1.5.25",
42
+ "@swc/core": "1.5.27",
43
43
  "@types/node": "20.14.2",
44
44
  "@types/xml2js": "0.4.14",
45
45
  "chalk": "5.3.0",
@@ -50,9 +50,9 @@
50
50
  "remark": "15.0.1",
51
51
  "remark-frontmatter": "5.0.0",
52
52
  "tsmatchers": "5.0.2",
53
- "tsx": "4.13.2",
53
+ "tsx": "4.15.1",
54
54
  "xml2js": "0.6.2",
55
- "yaml": "2.4.3"
55
+ "yaml": "2.4.5"
56
56
  },
57
57
  "lint-staged": {
58
58
  "*.{ts,tsx}": [