Added validation schema
ADD
src/assets/server/rest/v1/schemas/chargingstation/chargingprofile-create.json
+{
+ "title": "Create a Charging Profile",
+ "type": "object",
+ "properties": {
+ "chargingStationID": {
+ "$ref": "chargingstation.json#/definitions/id"
+ },
+ "connectorID": {
+ "$ref": "chargingstation.json#/definitions/connectorID"
+ },
+ "chargePointID": {
+ "type": "number",
+ "sanitize": "mongo"
+ },
+ "profile": {
+ "$ref": "chargingstation.json#/definitions/chargingProfile"
+ }
+ },
+ "required": [
+ "chargingStationID", "connectorID", "chargePointID", "profile"
+ ]
+}

MODIFY
src/assets/server/rest/v1/schemas/chargingstation/chargingstation.json
src/assets/server/rest/v1/schemas/chargingstation/chargingstation.json
"type": "object",
"properties": {
"chargingProfileId": {
- "$ref": "common.json#/definitions/id"
+ "type": "number",
+ "sanitize": "mongo"
},
"transactionId": {
"$ref": "transaction.json#/definitions/id"
},
"validFrom": {
"type": "string",
- "format": "date",
+ "format": "date-time",
"sanitize": "mongo"
},
"validTo": {
"type": "string",
- "format": "date",
+ "format": "date-time",
"sanitize": "mongo"
},
"chargingSchedule": {
"sanitize": "mongo"
}
},
- "required": ["chargingRateUnit", "chargingSchedulePeriod"]
+ "required": [
+ "chargingRateUnit",
+ "chargingSchedulePeriod"
+ ]
},
"chargingSchedulePeriod": {
- "type": "object",
+ "type": "array",
+ "items": {
"properties": {
"startPeriod": {
"type": "number",
"type": "number",
"sanitize": "mongo"
}
+ }
},
- "required": ["startPeriod", "limit"]
+ "required": [
+ "startPeriod",
+ "limit"
+ ]
},
"connectorID": {
"type": "number",
"sanitize": "mongo"
},
+ "connectorIDs": {
+ "type": "string",
+ "pattern": "^(\\d\\|?)*$",
+ "sanitize": "mongo"
+ },
"chargingProfilePurpose": {
"type": "string",
"sanitize": "mongo"

MODIFY
src/server/rest/v1/service/ChargingStationService.ts
src/server/rest/v1/service/ChargingStationService.ts
@@ -609,7 +609,7 @@ export default class ChargingStationService {
public static async handleCreateChargingProfile(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
// Filter
- const filteredRequest = ChargingStationSecurity.filterChargingProfileCreateRequest(req.body);
+ const filteredRequest = ChargingStationValidator.getInstance().validateChargingProfileCreateReq(req.body);
// Check existence
const chargingStation = await ChargingStationStorage.getChargingStation(req.user.tenantID, filteredRequest.chargingStationID);
UtilsService.assertObjectExists(action, chargingStation, `ChargingStation '${filteredRequest.chargingStationID}' does not exist.`,

MODIFY
src/server/rest/v1/service/security/ChargingStationSecurity.ts
src/server/rest/v1/service/security/ChargingStationSecurity.ts
@@ -194,23 +194,6 @@ export default class ChargingStationSecurity {
return filteredRequest;
}
- public static filterChargingProfileCreateRequest(request: any): ChargingProfile {
- const filteredRequest: ChargingProfile = {} as ChargingProfile;
- if (Utils.objectHasProperty(request, 'chargingStationID')) {
- filteredRequest.chargingStationID = sanitize(request.chargingStationID);
- }
- if (Utils.objectHasProperty(request, 'connectorID')) {
- filteredRequest.connectorID = sanitize(request.connectorID);
- }
- if (Utils.objectHasProperty(request, 'chargePointID')) {
- filteredRequest.chargePointID = sanitize(request.chargePointID);
- }
- if (Utils.objectHasProperty(request, 'profile')) {
- filteredRequest.profile = ChargingStationSecurity.filterChargingProfileRequest(request.profile);
- }
- return filteredRequest;
- }
-
public static filterChargingProfileUpdateRequest(request: any): ChargingProfile {
const filteredRequest: ChargingProfile = {} as ChargingProfile;
if (Utils.objectHasProperty(request, 'id')) {

MODIFY
src/server/rest/v1/validator/ChargingStationValidator.ts
src/server/rest/v1/validator/ChargingStationValidator.ts
-import { HttpChargingStationCommandRequest, HttpChargingStationsRequest } from '../../../../types/requests/HttpChargingStationRequest';
+import { HttpChargingProfilesRequest, HttpChargingStationCommandRequest, HttpChargingStationsRequest } from '../../../../types/requests/HttpChargingStationRequest';
+import { ChargingProfile } from '../../../../types/ChargingProfile';
import HttpByIDRequest from '../../../../types/requests/HttpByIDRequest';
import Schema from './Schema';
import SchemaValidator from './SchemaValidator';
@@ -12,6 +13,7 @@ export default class ChargingStationValidator extends SchemaValidator {
private chargingStationGet: Schema;
private chargingStationDelete: Schema;
private chargingStationAction: Schema;
+ private chargingProfileCreate: Schema;
private constructor() {
@@ -20,6 +22,7 @@ export default class ChargingStationValidator extends SchemaValidator {
this.chargingStationGet = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/chargingstation/chargingstation-get.json`, 'utf8'));
this.chargingStationDelete = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/chargingstation/chargingstation-delete.json`, 'utf8'));
this.chargingStationAction = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/chargingstation/chargingstation-action.json`, 'utf8'));
+ this.chargingProfileCreate = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/chargingstation/chargingprofile-create.json`, 'utf8'));
}
public static getInstance(): ChargingStationValidator {
@@ -52,4 +55,10 @@ export default class ChargingStationValidator extends SchemaValidator {
this.validate(this.chargingStationAction, data);
return data;
}
+
+ public validateChargingProfileCreateReq(data: any): ChargingProfile {
+ // Validate schema
+ this.validate(this.chargingProfileCreate, data);
+ return data;
+ }
}

Added validation schema