Do not coerce nulls to strings and numbers without explicit option

master
Vitaliy Filippov 2019-10-24 20:24:20 +03:00
parent 60c293637d
commit b9369ed663
2 changed files with 6 additions and 3 deletions

1
lib/ajv.d.ts vendored
View File

@ -179,6 +179,7 @@ declare namespace ajv {
loadSchema?: (uri: string, cb?: (err: Error, schema: object) => void) => PromiseLike<object | boolean>;
removeAdditional?: boolean | 'all' | 'failing';
useDefaults?: boolean | 'empty' | 'shared';
coerceNulls?: boolean;
coerceTypes?: boolean | 'array';
strictDefaults?: boolean | 'log';
strictKeywords?: boolean | 'log';

View File

@ -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' }}