Added charging station validator
ADD
src/assets/server/rest/v1/schemas/chargingstation/chargingstation.json
+{
+ "$id": "chargingstation.json",
+ "definitions": {
+ "chargingProfile": {
+ "type": "object",
+ "properties": {
+ "chargingProfileId": {
+ "$ref": "common.json#/definitions/id"
+ },
+ "transactionId": {
+ "$ref": "common.json#/definitions/id"
+ },
+ "stackLevel": {
+ "$ref": "#/definitions/stackLevel"
+ },
+ "chargingProfilePurpose": {
+ "$ref": "#/definitions/chargingProfilePurpose"
+ },
+ "chargingProfileKind": {
+ "type": "string",
+ "sanitize": "mongo"
+ },
+ "recurrencyKind": {
+ "type": "string",
+ "sanitize": "mongo"
+ },
+ "validFrom": {
+ "type": "date",
+ "sanitize": "mongo"
+ },
+ "validTo": {
+ "type": "date",
+ "sanitize": "mongo"
+ },
+ "chargingSchedule": {
+ "$ref": "#/definitions/chargingSchedule"
+ }
+ }
+ },
+ "chargingSchedule": {
+ "type": "object",
+ "properties": {
+ "duration": {
+ "type": "number",
+ "sanitize": "mongo"
+ },
+ "startSchedule": {
+ "type": "date",
+ "sanitize": "mongo"
+ },
+ "chargingRateUnit": {
+ "type": "string",
+ "sanitize": "mongo"
+ },
+ "chargingSchedulePeriod": {
+ "$ref": "#/definitions/chargingSchedulePeriod"
+ },
+ "minChargeRate": {
+ "type": "number",
+ "sanitize": "mongo"
+ }
+ },
+ "required": ["chargingRateUnit", "chargingSchedulePeriod"]
+ },
+ "chargingSchedulePeriod": {
+ "type": "object",
+ "properties": {
+ "startPeriod": {
+ "type": "number",
+ "sanitize": "mongo"
+ },
+ "limit": {
+ "type": "number",
+ "sanitize": "mongo"
+ },
+ "numberPhases": {
+ "type": "number",
+ "sanitize": "mongo"
+ }
+ },
+ "required": ["startPeriod", "limit"]
+ },
+ "connectorID": {
+ "type": "number",
+ "sanitize": "mongo"
+ },
+ "chargingProfilePurpose": {
+ "type": "string",
+ "sanitize": "mongo"
+ },
+ "stackLevel": {
+ "type": "number",
+ "sanitize": "mongo"
+ }
+ }
+}

ADD
src/assets/server/rest/v1/schemas/chargingstation/chargingstations-get.json
+{
+ "title": "Get Charging Stations Request",
+ "type": "object",
+ "properties": {
+ "Issuer": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ },
+ "Search": {
+ "$ref": "common.json#/definitions/search"
+ },
+ "WithNoSiteArea": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ },
+ "SiteID": {
+ "$ref": "common.json#/definitions/id"
+ },
+ "WithSite": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ },
+ "SiteAreaID": {
+ "$ref": "common.json#/definitions/id"
+ },
+ "ConnectorStatus": {
+ "type": "string",
+ "sanitize": "mongo"
+ },
+ "ConnectorType": {
+ "type": "string",
+ "sanitize": "mongo"
+ },
+ "ChargeBoxID": {
+ "type": "string",
+ "sanitize": "mongo"
+ },
+ "IncludeDeleted": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ },
+ "ErrorType": {
+ "type": "string",
+ "sanitize": "mongo"
+ },
+ "LocLongitude": {
+ "type": "number",
+ "sanitize": "mongo"
+ },
+ "LocLatitude": {
+ "type": "number",
+ "sanitize": "mongo"
+ },
+ "LocMaxDistanceMeters": {
+ "type": "number",
+ "sanitize": "mongo",
+ "exclusiveMinimum": 0
+ },
+ "Limit": {
+ "$ref": "common.json#/definitions/limit"
+ },
+ "SortFields": {
+ "$ref": "common.json#/definitions/sortFields"
+ },
+ "Skip": {
+ "$ref": "common.json#/definitions/skip"
+ },
+ "OnlyRecordCount": {
+ "$ref": "common.json#/definitions/onlyRecordCount"
+ },
+ "ProjectFields": {
+ "$ref": "common.json#/definitions/projectFields"
+ }
+ },
+ "required": []
+}

MODIFY
src/assets/server/rest/v1/schemas/common/common.json
src/assets/server/rest/v1/schemas/common/common.json
"type": "string",
"sanitize": "mongo"
},
+ "onlyRecordCount": {
+ "type": "boolean",
+ "sanitize": "mongo"
+ },
"skip": {
"type": "number",
"sanitize": "mongo"

MODIFY
src/server/rest/v1/service/ChargingStationService.ts
src/server/rest/v1/service/ChargingStationService.ts
@@ -13,6 +13,7 @@ import ChargingStationClientFactory from '../../../../client/ocpp/ChargingStatio
import { ChargingStationInErrorType } from '../../../../types/InError';
import ChargingStationSecurity from './security/ChargingStationSecurity';
import ChargingStationStorage from '../../../../storage/mongodb/ChargingStationStorage';
+import ChargingStationValidator from '../validator/ChargingStationValidator';
import ChargingStationVendorFactory from '../../../../integration/charging-station-vendor/ChargingStationVendorFactory';
import Constants from '../../../../utils/Constants';
import CpoOCPIClient from '../../../../client/ocpi/CpoOCPIClient';
@@ -1341,7 +1342,7 @@ export default class ChargingStationService {
});
}
// Filter
- const filteredRequest = ChargingStationSecurity.filterChargingStationsRequest(req.query);
+ const filteredRequest = ChargingStationValidator.getInstance().validateChargingStationsGetReq(req.query);
// Check Users
let userProject: string[] = [];
if (Authorizations.canListUsers(req.user)) {
@@ -1365,8 +1366,9 @@ export default class ChargingStationService {
];
}
// Check projection
- if (!Utils.isEmptyArray(filteredRequest.ProjectFields)) {
- projectFields = projectFields.filter((projectField) => filteredRequest.ProjectFields.includes(projectField));
+ const httpProjectFields = UtilsService.httpFilterProjectToMongoDB(filteredRequest.ProjectFields);
+ if (!Utils.isEmptyArray(httpProjectFields)) {
+ projectFields = projectFields.filter((projectField) => httpProjectFields.includes(projectField));
}
// Get Charging Stations
const chargingStations = await ChargingStationStorage.getChargingStations(req.user.tenantID,
@@ -1387,7 +1389,7 @@ export default class ChargingStationService {
{
limit: filteredRequest.Limit,
skip: filteredRequest.Skip,
- sort: filteredRequest.SortFields,
+ sort: UtilsService.httpSortFieldsToMongoDB(filteredRequest.SortFields),
onlyRecordCount: filteredRequest.OnlyRecordCount
},
projectFields

ADD
src/server/rest/v1/validator/ChargingStationValidator.ts
+import { HttpChargingStationsRequest } from '../../../../types/requests/HttpChargingStationRequest';
+import Schema from './Schema';
+import SchemaValidator from './SchemaValidator';
+import fs from 'fs';
+import global from '../../../../types/GlobalType';
+
+export default class ChargingStationValidator extends SchemaValidator {
+ private static _instance: ChargingStationValidator | undefined;
+ private chargingStationsGet: Schema;
+
+
+ private constructor() {
+ super('ChargingStationValidator');
+ this.chargingStationsGet = JSON.parse(fs.readFileSync(`${global.appRoot}/assets/server/rest/v1/schemas/chargingstation/chargingstations-get.json`, 'utf8'));
+ }
+
+ public static getInstance(): ChargingStationValidator {
+ if (!ChargingStationValidator._instance) {
+ ChargingStationValidator._instance = new ChargingStationValidator();
+ }
+ return ChargingStationValidator._instance;
+ }
+
+ public validateChargingStationsGetReq(data: any): HttpChargingStationsRequest {
+ // Validate schema
+ this.validate(this.chargingStationsGet, data);
+ // Validate deps between components
+ return data;
+ }
+}

Added charging station validator