Skip to content

Commit

Permalink
Merge pull request #1235 from solaris-games/dev
Browse files Browse the repository at this point in the history
Update 248 fix
  • Loading branch information
SpacialCircumstances authored Nov 20, 2024
2 parents 7b12df4 + 230a45a commit 3ea4c6f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
4 changes: 2 additions & 2 deletions server/api/controllers/carrier.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { DependencyContainer } from '../../services/types/DependencyContainer';
import { mapToCarrierCalculateCombatRequest, parseCarrierLoopWaypointsRequest, mapToCarrierRenameCarrierRequest, parseCarierSaveWaypointsRequest, parseCarrierTransferShipsRequest } from '../requests/carrier';
import { mapToCarrierCalculateCombatRequest, parseCarrierLoopWaypointsRequest, mapToCarrierRenameCarrierRequest, parseCarrierSaveWaypointsRequest, parseCarrierTransferShipsRequest } from '../requests/carrier';

export default (container: DependencyContainer) => {
return {
saveWaypoints: async (req, res, next) => {
try {
const reqObj = parseCarierSaveWaypointsRequest(req.body);
const reqObj = parseCarrierSaveWaypointsRequest(req.body);

let report = await container.waypointService.saveWaypoints(
req.game,
Expand Down
22 changes: 18 additions & 4 deletions server/api/requests/carrier.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
import ValidationError from "../../errors/validation";
import { CarrierWaypointActionType, CarrierWaypointActionTypes } from "../../services/types/CarrierWaypoint";
import { DBObjectId } from "../../services/types/DBObjectId";
import { array, boolean, map, number, numberAdv, object, objectId, or, positiveInteger, string, stringEnumeration, Validator } from "../validate";
import {
array,
boolean,
map,
number,
numberAdv,
object,
objectId,
or,
positiveInteger,
string,
stringEnumeration,
Validator,
withDefault
} from "../validate";
import { keyHasArrayValue, keyHasBooleanValue, keyHasNumberValue, keyHasObjectValue, keyHasStringValue } from "./helpers";

type CarrierSaveWaypoint = {
Expand All @@ -17,13 +31,13 @@ export type CarrierSaveWaypointsRequest = {
looped: boolean;
};

export const parseCarierSaveWaypointsRequest: Validator<CarrierSaveWaypointsRequest> = object({
export const parseCarrierSaveWaypointsRequest: Validator<CarrierSaveWaypointsRequest> = object({
waypoints: array(object({
source: objectId,
destination: objectId,
action: stringEnumeration<CarrierWaypointActionType, CarrierWaypointActionType[]>(CarrierWaypointActionTypes),
actionShips: map((a) => a || 0, positiveInteger),
delayTicks: map((a) => a || 0, positiveInteger),
actionShips: withDefault(0, positiveInteger),
delayTicks: withDefault(0, positiveInteger),
})),
looped: boolean,
});
Expand Down
22 changes: 18 additions & 4 deletions server/api/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { DBObjectId, objectIdFromString } from "../services/types/DBObjectId";
export type Validator<T> = (value: any) => T;

const failed = (expected: string, value: any) => {
return new ValidationError(`Expected ${expected}, but got: ${typeof value}`);
return new ValidationError(`Expected ${expected}, but got: ${value.toString().substring(1000)} ${typeof value}`);
}

const primitive = (t: string) => (value: any) => {
Expand Down Expand Up @@ -129,8 +129,12 @@ export const object = <T>(objValidator: ObjectValidator<T>): Validator<T> => {
let n: any = {};

for (const key of Object.keys(objValidator)) {
const validator: Validator<any> = objValidator[key];
n[key] = validator(v[key]);
try {
const validator: Validator<any> = objValidator[key];
n[key] = validator(v[key]);
} catch (e) {
throw new ValidationError(`Error in field ${key}: ${e.message}`);
}
}

return n;
Expand Down Expand Up @@ -193,4 +197,14 @@ export const numberAdv = (props: NumberValidationProps) => v => {
export const positiveInteger = numberAdv({
integer: true,
sign: 'positive'
});
});

export const withDefault = <A>(defaultValue: A, validator: Validator<A>): Validator<A> => {
return v => {
if (v === undefined || v === null) {
return defaultValue;
}

return validator(v);
}
}

0 comments on commit 3ea4c6f

Please sign in to comment.