diff --git a/lib/ajv.d.ts b/lib/ajv.d.ts index 5e7cfa7..5c7dbe1 100644 --- a/lib/ajv.d.ts +++ b/lib/ajv.d.ts @@ -179,6 +179,7 @@ declare namespace ajv { loadSchema?: (uri: string, cb?: (err: Error, schema: object) => void) => PromiseLike; removeAdditional?: boolean | 'all' | 'failing'; useDefaults?: boolean | 'empty' | 'shared'; + coerceNulls?: boolean; coerceTypes?: boolean | 'array'; strictDefaults?: boolean | 'log'; strictKeywords?: boolean | 'log'; diff --git a/lib/dot/coerce.def b/lib/dot/coerce.def index 86e0e18..6c7b09f 100644 --- a/lib/dot/coerce.def +++ b/lib/dot/coerce.def @@ -28,10 +28,12 @@ {{? $type == 'string' }} if ({{=$dataType}} == 'number' || {{=$dataType}} == 'boolean') {{=$coerced}} = '' + {{=$data}}; + {{? it.opts.coerceNulls }} else if ({{=$data}} === null) {{=$coerced}} = ''; + {{?}} {{?? $type == 'number' || $type == 'integer' }} - if ({{=$dataType}} == 'boolean' || {{=$data}} === null - || ({{=$dataType}} == 'string' && {{=$data}} && {{=$data}} == +{{=$data}} + if ({{? it.opts.coerceNulls }}{{=$data}} === null ||{{?}} + ({{=$dataType}} == 'string' && {{=$data}} && {{=$data}} == +{{=$data}} {{? $type == 'integer' }} && !({{=$data}} % 1){{?}})) {{=$coerced}} = +{{=$data}}; {{?? $type == 'boolean' }} @@ -39,7 +41,7 @@ {{=$coerced}} = false; else if ({{=$data}} === 'true' || {{=$data}} === 1) {{=$coerced}} = true; - {{?? $type == 'null' }} + {{?? $type == 'null' && it.opts.coerceNulls }} if ({{=$data}} === '' || {{=$data}} === 0 || {{=$data}} === false) {{=$coerced}} = null; {{?? it.opts.coerceTypes == 'array' && $type == 'array' }}