Add object property validation in parser #369

This commit introduces stricter type validation across the application
to reject objects with unexpected properties, enhancing the robustness
and predictability of data handling.

Changes include:

- Implement a common utility to validate object types.
- Refactor across various parsers and data handlers to utilize the new
  validations.
- Update error messages for better clarity and troubleshooting.
This commit is contained in:
undergroundwires
2024-06-13 22:26:57 +02:00
parent c138f74460
commit 6ecfa9b954
43 changed files with 1215 additions and 466 deletions

View File

@@ -0,0 +1,23 @@
import type { CategoryCollectionFactory } from '@/application/Parser/CategoryCollectionParser';
import type { CategoryCollectionInitParameters } from '@/domain/CategoryCollection';
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import { CategoryCollectionStub } from './CategoryCollectionStub';
export function createCategoryCollectionFactorySpy(): {
readonly categoryCollectionFactorySpy: CategoryCollectionFactory;
getInitParameters: (
category: ICategoryCollection,
) => CategoryCollectionInitParameters | undefined;
} {
const createdCategoryCollections = new Map<
ICategoryCollection, CategoryCollectionInitParameters
>();
return {
categoryCollectionFactorySpy: (parameters) => {
const categoryCollection = new CategoryCollectionStub();
createdCategoryCollections.set(categoryCollection, parameters);
return categoryCollection;
},
getInitParameters: (category) => createdCategoryCollections.get(category),
};
}

View File

@@ -2,8 +2,8 @@ import type { ProjectDetails } from '@/domain/Project/ProjectDetails';
import type { ICategoryCollection } from '@/domain/ICategoryCollection';
import { getEnumValues } from '@/application/Common/Enum';
import type { CollectionData } from '@/application/collections/';
import type { CategoryCollectionParserType } from '@/application/Parser/ApplicationParser';
import { OperatingSystem } from '@/domain/OperatingSystem';
import type { CategoryCollectionParser } from '@/application/Parser/CategoryCollectionParser';
import { CategoryCollectionStub } from './CategoryCollectionStub';
export class CategoryCollectionParserStub {
@@ -22,7 +22,7 @@ export class CategoryCollectionParserStub {
return this;
}
public getStub(): CategoryCollectionParserType {
public getStub(): CategoryCollectionParser {
return (data: CollectionData, projectDetails: ProjectDetails): ICategoryCollection => {
this.arguments.push({ data, projectDetails });
const foundReturnValue = this.returnValues.get(data);

View File

@@ -0,0 +1,34 @@
import type { CategoryParser } from '@/application/Parser/Executable/CategoryParser';
import type { CategoryData } from '@/application/collections/';
import type { Category } from '@/domain/Executables/Category/Category';
import type { CategoryCollectionSpecificUtilities } from '@/application/Parser/Executable/CategoryCollectionSpecificUtilities';
import { CategoryStub } from './CategoryStub';
export class CategoryParserStub {
private configuredParseResults = new Map<CategoryData, Category>();
private usedUtilities = new Array<CategoryCollectionSpecificUtilities>();
public get(): CategoryParser {
return (category, utilities) => {
const result = this.configuredParseResults.get(category);
this.usedUtilities.push(utilities);
if (result) {
return result;
}
return new CategoryStub(5489);
};
}
public withConfiguredParseResult(
givenCategory: CategoryData,
parsedCategory: Category,
): this {
this.configuredParseResults.set(givenCategory, parsedCategory);
return this;
}
public getUsedUtilities(): readonly CategoryCollectionSpecificUtilities[] {
return this.usedUtilities;
}
}

View File

@@ -1,17 +1,17 @@
import type { IEnumParser } from '@/application/Common/Enum';
import type { EnumParser } from '@/application/Common/Enum';
export class EnumParserStub<T> implements IEnumParser<T> {
export class EnumParserStub<T> implements EnumParser<T> {
private readonly scenarios = new Array<{
inputName: string, inputValue: string, outputValue: T }>();
private defaultValue: T;
public setup(inputName: string, inputValue: string, outputValue: T) {
public setup(inputName: string, inputValue: string, outputValue: T): this {
this.scenarios.push({ inputName, inputValue, outputValue });
return this;
}
public setupDefaultValue(outputValue: T) {
public setupDefaultValue(outputValue: T): this {
this.defaultValue = outputValue;
return this;
}

View File

@@ -1,4 +1,4 @@
import type { ErrorWithContextWrapper } from '@/application/Parser/ContextualError';
import type { ErrorWithContextWrapper } from '@/application/Parser/Common/ContextualError';
export const errorWithContextWrapperStub
: ErrorWithContextWrapper = (error, message) => new Error(`[stubbed error wrapper] ${error.message} + ${message}`);

View File

@@ -1,4 +1,4 @@
import type { ErrorWithContextWrapper } from '@/application/Parser/ContextualError';
import type { ErrorWithContextWrapper } from '@/application/Parser/Common/ContextualError';
export class ErrorWrapperStub {
private errorToReturn: Error | undefined;

View File

@@ -1,5 +1,5 @@
import type { ExecutableData } from '@/application/collections/';
import type { ExecutableValidator, ExecutableValidatorFactory } from '@/application/Parser/Executable/Validation/ExecutableValidator';
import type { TypeValidator } from '@/application/Parser/Common/TypeValidator';
import { StubWithObservableMethodCalls } from './StubWithObservableMethodCalls';
export const createExecutableValidatorFactoryStub
@@ -23,10 +23,10 @@ export class ExecutableValidatorStub
return this;
}
public assertDefined(data: ExecutableData): this {
public assertType(assert: (validator: TypeValidator) => void): this {
this.registerMethodCall({
methodName: 'assertDefined',
args: [data],
methodName: 'assertType',
args: [assert],
});
return this;
}

View File

@@ -0,0 +1,45 @@
import type { NonEmptyCollectionAssertion, ObjectAssertion, TypeValidator } from '@/application/Parser/Common/TypeValidator';
import type { FunctionKeys } from '@/TypeHelpers';
import { expectDeepIncludes } from '@tests/shared/Assertions/ExpectDeepIncludes';
import { StubWithObservableMethodCalls } from './StubWithObservableMethodCalls';
export type UnknownObjectAssertion = ObjectAssertion<unknown>;
export class TypeValidatorStub
extends StubWithObservableMethodCalls<TypeValidator>
implements TypeValidator {
public assertObject<T>(assertion: ObjectAssertion<T>): void {
this.registerMethodCall({
methodName: 'assertObject',
args: [assertion as UnknownObjectAssertion],
});
}
public assertNonEmptyCollection(assertion: NonEmptyCollectionAssertion): void {
this.registerMethodCall({
methodName: 'assertNonEmptyCollection',
args: [assertion],
});
}
public expectObjectAssertion<T>(
expectedAssertion: ObjectAssertion<T>,
): void {
this.expectAssertion('assertObject', expectedAssertion as UnknownObjectAssertion);
}
public expectNonEmptyCollectionAssertion(
expectedAssertion: NonEmptyCollectionAssertion,
): void {
this.expectAssertion('assertNonEmptyCollection', expectedAssertion);
}
private expectAssertion<T extends FunctionKeys<TypeValidator>>(
methodName: T,
expectedAssertion: Parameters<TypeValidator[T]>[0],
): void {
const assertionCalls = this.callHistory.filter((c) => c.methodName === methodName);
const assertionArgs = assertionCalls.map((c) => c.args[0]);
expectDeepIncludes(assertionArgs, expectedAssertion);
}
}