From 89812b366ba1288a5191fd4ef1cdd283aa5f85c8 Mon Sep 17 00:00:00 2001 From: Gadi Cohen Date: Sat, 6 Feb 2021 06:40:23 +0200 Subject: [PATCH] chore(validate): coerce numbers from strings --- src/lib/validateAndCoerceTypes.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/lib/validateAndCoerceTypes.ts b/src/lib/validateAndCoerceTypes.ts index 4e600cea..9c956543 100644 --- a/src/lib/validateAndCoerceTypes.ts +++ b/src/lib/validateAndCoerceTypes.ts @@ -32,6 +32,13 @@ ajv.addKeyword({ if (typeof data === 'number') return true; + if (typeof data === 'string') { + let float = Number.parseFloat(data); + if (Number.isNaN(float)) + return false; + return set(float); + } + if (typeof data === 'object') { if (Object.keys(data).length === 0) return set(null);