(schema: TObject, data: any)
| 9 | * @throws {Error} - If the data does not match the schema |
| 10 | */ |
| 11 | export const validateData = <T>(schema: TObject, data: any): T => { |
| 12 | const C = TypeCompiler.Compile(schema); |
| 13 | // remove extra fields from data |
| 14 | const properties = schema.properties; |
| 15 | for (const key in data) { |
| 16 | if (data.hasOwnProperty(key) && !properties?.[key]) { |
| 17 | delete data[key]; |
| 18 | } |
| 19 | } |
| 20 | const isValid = C.Check(data); |
| 21 | if (isValid) { |
| 22 | return data as T; |
| 23 | } |
| 24 | throw new Error(JSON.stringify([...C.Errors(data)].map(({ path, message }) => ({ path, message })))); |
| 25 | }; |
no outgoing calls
no test coverage detected