Skip to content

Commit

Permalink
Merge pull request #1 from hyperifyio/test-function
Browse files Browse the repository at this point in the history
Entity test function
  • Loading branch information
thejhh authored Dec 22, 2023
2 parents 949169f + 3610ee7 commit 5790461
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions entities/types/EntityFactoryImpl.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright (c) 2023. Sendanor <info@sendanor.fi>. All rights reserved.

import { isObject } from "../../types/Object";
import { EnumUtils } from "../../EnumUtils";
import { filter } from "../../functions/filter";
import { forEach } from "../../functions/forEach";
Expand Down Expand Up @@ -858,6 +859,41 @@ export class EntityFactoryImpl<
);
}

public createTestFunction () {
const properties : readonly EntityProperty[] = this.getProperties();

// const propertyNames : readonly string[] = map(
// properties,
// (item : EntityProperty) : string => item.getPropertyName()
// );

const methods = [
...properties.map((prop: EntityProperty) : readonly string[] => prop.getMethodAliases()),
...properties.map((prop: EntityProperty) : readonly string[] => prop.getGetterNames()),
...properties.map((prop: EntityProperty) : readonly string[] => prop.getSetterNames())
].flat();

const checkProperties = reduce(
properties,
(prev: PropertyTypeCheckFn, item: EntityProperty): PropertyTypeCheckFn => {
const propertyName = item.getPropertyName();
const isType = EntityFactoryImpl.createTypeCheckFn(...item.getTypes());
return (value: ReadonlyJsonObject) : boolean => prev(value) && isType(value[propertyName]);
},
(value: ReadonlyJsonObject): boolean => isObject(value),
);

return (value : unknown) : value is D => {
return (
isRegularObject(value)
&& hasNoOtherKeysInDevelopment(value, propertyNames)
&& checkProperties(value)
);
};


}


/**
* @inheritDoc
Expand Down

0 comments on commit 5790461

Please sign in to comment.