MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / validateProperty

Function validateProperty

packages/core/src/entity/validators.ts:8–27  ·  view source on GitHub ↗
(prop: EntityProperty, givenValue: any, entity: T)

Source from the content-addressed store, hash-verified

6
7/** @internal */
8export function validateProperty<T extends object>(prop: EntityProperty, givenValue: any, entity: T): void {
9 if (givenValue == null || isRaw(givenValue)) {
10 return;
11 }
12
13 const expectedType = prop.runtimeType;
14 const propName = prop.embedded ? prop.name.replace(/~/g, '.') : prop.name;
15 const givenType = Utils.getObjectType(givenValue);
16
17 if (prop.enum && prop.items) {
18 /* v8 ignore next */
19 if (!prop.items.some(it => it === givenValue)) {
20 throw ValidationError.fromWrongPropertyType(entity, propName, expectedType, givenType, givenValue);
21 }
22 } else {
23 if (givenType !== expectedType && SCALAR_TYPES.has(expectedType)) {
24 throw ValidationError.fromWrongPropertyType(entity, propName, expectedType, givenType, givenValue);
25 }
26 }
27}
28
29function getValue(o: Dictionary, prop: EntityProperty) {
30 if (prop.embedded && prop.embedded[0] in o) {

Callers 3

mapMethod · 0.85
assignPropertyMethod · 0.85
validateEntityFunction · 0.85

Calls 4

isRawFunction · 0.85
getObjectTypeMethod · 0.80
fromWrongPropertyTypeMethod · 0.80
hasMethod · 0.65

Tested by

no test coverage detected