(prop: EntityProperty, givenValue: any, entity: T)
| 6 | |
| 7 | /** @internal */ |
| 8 | export 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 | |
| 29 | function getValue(o: Dictionary, prop: EntityProperty) { |
| 30 | if (prop.embedded && prop.embedded[0] in o) { |
no test coverage detected