-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cds-plugin.js
208 lines (197 loc) · 5.42 KB
/
cds-plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict';
const cds = require('@sap/cds');
const Ajv = require('ajv');
const ajvFormats = require('ajv-formats');
const ajv = new Ajv({allErrors: true, coerceTypes: true});
ajvFormats(ajv);
const logger = cds.log('validator-plugin');
const VALIDATE_EVENTS = ['CREATE', 'UPDATE'];
let regxpAnnotationTag = new RegExp(`^@Validator.`);
const cdsTypeToAjvType = new Map([
['cds.UUID', 'string'],
['cds.Boolean', 'boolean'],
['cds.UInt8', 'integer'],
['cds.Int16', 'integer'],
['cds.Int32', 'integer'],
['cds.Integer', 'integer'],
['cds.Int64', 'integer'],
['cds.Integer64', 'integer'],
['cds.Decimal', 'number'],
['cds.Double', 'number'],
['cds.Date', 'string'],
['cds.Time', 'string'],
['cds.DateTime', 'string'],
['cds.Timestamp', 'string'],
['cds.String', 'string'],
['cds.Binary', 'string'],
['cds.LargeBinary', 'string'],
['cds.LargeString', 'string']
]);
/**
* Builds schema to validate an entity. Ensures only the annotated fields are validated.
* @param {cds.entity} entity
* @returns {Ajv.SchemaObject}
*/
function buildSchema(entity) {
const schema = {
$id: entity.name,
$schema: 'http://json-schema.org/draft-07/schema',
type: 'object',
properties: {},
// optionalProperties: {}, // JTD
additionalProperties: true
};
for (const [key, elementProperties] of Object.entries(entity.elements)) {
const schemaProperty = buildSchemaProperty(elementProperties);
if (Object.keys(schemaProperty).length) {
schema.properties[key] = schemaProperty;
// schema.optionalProperties[key] = schemaProperty; // JTD
}
}
logger.debug(schema);
return schema;
}
/**
* Build schema property from element properties
* @param {object} elementProperties
* @returns {object}
*/
function buildSchemaProperty(elementProperties) {
const property = {};
for (const [key, value] of Object.entries(elementProperties)) {
if (isValidAnnotation(key)) {
property.type = cdsTypeToAjvType.get(elementProperties.type);
const tag = key.split('.')[1];
if (tag) {
property[tag] = value;
}
}
}
return property;
}
/**
* Map AJV errors to CAP format to be used in req.error()
* @param {Ajv.ErrorObject[]} ajvErrors
* @returns {object[]}
*/
function mapAjvToCdsErrors(ajvErrors) {
logger.debug('Validation errors:', ajvErrors?.length);
const cdsError = [{
code: 40099,
message: 'Payload invalid!',
status: 400
}];
for (const err of ajvErrors) {
cdsError.push({
code: 40099,
message: err.message,
target: err.instancePath,
status: 400
});
}
return cdsError;
}
/**
* Get entity's annotated elements
* @param {cds.ApplicationService} service
* @returns {cds.entity[]}
*/
function getAnnotatedEntities(service) {
const annotatedEntities = [];
for (const entity of service.entities) {
logger.debug('Looking for annotations in:', entity.name);
if (hasAnnotatedElement(entity.elements)) {
annotatedEntities.push(entity);
}
}
return annotatedEntities;
}
/**
* Check whether entity has annotated elements
* @param {object} entityElements
* @returns {boolean}
*/
function hasAnnotatedElement(entityElements) {
for (const [key, elementProperties] of Object.entries(entityElements)) {
if (isElementAnnotated(elementProperties)) {
logger.debug('Annotation found in:', key);
return true;
}
}
return false;
}
/**
* Check whether entity element is annotated
* @param {object} elementProperties
* @returns {boolean}
*/
function isElementAnnotated(elementProperties) {
for (const key of Object.keys(elementProperties)) {
if (isValidAnnotation(key)) {
return true;
}
}
return false;
}
/**
* Check whether property is a valid annotation
* @param {string} property
* @returns {boolean}
*/
function isValidAnnotation(property) {
return regxpAnnotationTag.test(property);
}
/**
* Register request handler
* @param {cds.ApplicationService} service
* @param {cds.entity} entity
*/
function registerValidator(service, entity) {
logger.debug('Preparing validation schema to:', entity.name);
const schema = buildSchema(entity);
try {
ajv.compile(schema);
service.prepend(() => {
service.before(VALIDATE_EVENTS, entity, requestHandler);
});
} catch (err) {
logger.error(entity.name, 'schema is invalid! It will not be registered.');
logger.error(schema);
}
}
/**
* Validator Request Handler
* @param {cds.Request} req
*/
function requestHandler(req) {
logger.debug('Validating request to:', req.entity);
const validate = ajv.getSchema(req.entity);
if (!validate(req.data)) {
for (const err of mapAjvToCdsErrors(validate.errors)) {
req.error(err);
}
}
}
/**
* Event triggered when all CDS services are served. Validator middleware will be registered.
*/
cds.once('served', async (services) => {
logger.debug('Starting validator plugin...');
const annotatedServiceEntities = new Map();
for (const service of services) {
if (service.kind === 'app-service') {
annotatedServiceEntities.set(service, getAnnotatedEntities(service));
}
}
if (annotatedServiceEntities.size === 0) {
logger.debug('No annotations found. Validator will not process any request.');
}
for (const [service, entities] of annotatedServiceEntities) {
for (const entity of entities) {
registerValidator(service, entity);
}
}
regxpAnnotationTag = null;
cdsTypeToAjvType.clear();
annotatedServiceEntities.clear();
});