Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,14 @@ export const IsRecord = (x: unknown): x is Record<string, unknown> => {
// checks if value is (nested) object
return x !== null && typeof x === "object" && !Array.isArray(x);
};

export const IsValueUnitObject = (
x: unknown,
): x is { value?: unknown; unit?: unknown } => {
// checks if the argument is object and contains either property 'value' or 'unit'
return IsRecord(x) && ("value" in x || "unit" in x);
};

export const extractMetadataKeys = <T>(
instances: T[],
prop: keyof T,
Expand Down
10 changes: 3 additions & 7 deletions src/datasets/datasets.v4.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import { Request } from "express";
import { MongoError } from "mongodb";
import * as jmp from "json-merge-patch";
import { IsRecord } from "../common/utils";
import { IsRecord, IsValueUnitObject } from "../common/utils";
import { DatasetsService } from "./datasets.service";
import { DatasetClass, DatasetDocument } from "./schemas/dataset.schema";
import { PoliciesGuard } from "src/casl/guards/policies.guard";
Expand Down Expand Up @@ -237,14 +237,10 @@
return filter;
}

isValueUnitObject(obj: unknown): obj is { value?: unknown; unit?: unknown } {
// checks if the argument is object and contains either property 'value' or 'unit'
return IsRecord(obj) && ("value" in obj || "unit" in obj);
}

findInvalidValueUnitUpdates(
updateDto: Record<string, unknown>,
dataset: Record<string, unknown>,

Check failure on line 243 in src/datasets/datasets.v4.controller.ts

View workflow job for this annotation

GitHub Actions / eslint

Delete `⏎`
path: string[] = [],
): string[] {
// collect properties that have both 'value' and 'unit' in original dataset but one of either is missing in the updateDto body
Expand All @@ -253,12 +249,12 @@
for (const key in updateDto) {
const value = updateDto[key];
const currentPath = [...path, key];
if (this.isValueUnitObject(value)) {
if (IsValueUnitObject(value)) {
const datasetAtKey = currentPath.reduce<unknown>(
(obj, k) => (IsRecord(obj) ? obj[k] : undefined),
dataset,
);
if (this.isValueUnitObject(datasetAtKey)) {
if (IsValueUnitObject(datasetAtKey)) {
// check if current object's 'value' or 'unit' are not undefined in original dataset and passed updateDto
const originalHasValue = datasetAtKey.value !== undefined;
const originalHasUnit = datasetAtKey.unit !== undefined;
Expand Down
2 changes: 2 additions & 0 deletions test/config/.env.override
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
MONGODB_URI=mongodb://mongodb:27017/scicat-test-db
ES_HOST=http://elastic:9200
Loading