2023-12-18 13:12:25 +08:00
|
|
|
{{## def.coerceType:
|
|
|
|
{{
|
|
|
|
var $dataType = 'dataType' + $lvl
|
|
|
|
, $coerced = 'coerced' + $lvl;
|
|
|
|
}}
|
|
|
|
var {{=$dataType}} = typeof {{=$data}};
|
|
|
|
var {{=$coerced}} = undefined;
|
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
{{? it.opts.coerceTypes == 'array' }}
|
|
|
|
if ({{=$dataType}} == 'object' && Array.isArray({{=$data}}) && {{=$data}}.length == 1) {
|
|
|
|
{{=$data}} = {{=$data}}[0];
|
|
|
|
{{=$dataType}} = typeof {{=$data}};
|
|
|
|
if ({{=it.util.checkDataType(it.schema.type, $data, it.opts.strictNumbers)}}) {{=$coerced}} = {{=$data}};
|
|
|
|
}
|
|
|
|
{{?}}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
2024-01-16 21:26:16 +08:00
|
|
|
if ({{=$coerced}} !== undefined) ;
|
|
|
|
{{~ $coerceToTypes:$type:$i }}
|
2023-12-18 13:12:25 +08:00
|
|
|
{{? $type == 'string' }}
|
2024-01-16 21:26:16 +08:00
|
|
|
else if ({{=$dataType}} == 'number' || {{=$dataType}} == 'boolean')
|
2023-12-18 13:12:25 +08:00
|
|
|
{{=$coerced}} = '' + {{=$data}};
|
|
|
|
else if ({{=$data}} === null) {{=$coerced}} = '';
|
|
|
|
{{?? $type == 'number' || $type == 'integer' }}
|
2024-01-16 21:26:16 +08:00
|
|
|
else if ({{=$dataType}} == 'boolean' || {{=$data}} === null
|
2023-12-18 13:12:25 +08:00
|
|
|
|| ({{=$dataType}} == 'string' && {{=$data}} && {{=$data}} == +{{=$data}}
|
|
|
|
{{? $type == 'integer' }} && !({{=$data}} % 1){{?}}))
|
|
|
|
{{=$coerced}} = +{{=$data}};
|
|
|
|
{{?? $type == 'boolean' }}
|
2024-01-16 21:26:16 +08:00
|
|
|
else if ({{=$data}} === 'false' || {{=$data}} === 0 || {{=$data}} === null)
|
2023-12-18 13:12:25 +08:00
|
|
|
{{=$coerced}} = false;
|
|
|
|
else if ({{=$data}} === 'true' || {{=$data}} === 1)
|
|
|
|
{{=$coerced}} = true;
|
|
|
|
{{?? $type == 'null' }}
|
2024-01-16 21:26:16 +08:00
|
|
|
else if ({{=$data}} === '' || {{=$data}} === 0 || {{=$data}} === false)
|
2023-12-18 13:12:25 +08:00
|
|
|
{{=$coerced}} = null;
|
|
|
|
{{?? it.opts.coerceTypes == 'array' && $type == 'array' }}
|
2024-01-16 21:26:16 +08:00
|
|
|
else if ({{=$dataType}} == 'string' || {{=$dataType}} == 'number' || {{=$dataType}} == 'boolean' || {{=$data}} == null)
|
2023-12-18 13:12:25 +08:00
|
|
|
{{=$coerced}} = [{{=$data}}];
|
|
|
|
{{?}}
|
|
|
|
{{~}}
|
2024-01-16 21:26:16 +08:00
|
|
|
else {
|
2023-12-18 13:12:25 +08:00
|
|
|
{{# def.error:'type' }}
|
2024-01-16 21:26:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if ({{=$coerced}} !== undefined) {
|
2023-12-18 13:12:25 +08:00
|
|
|
{{# def.setParentData }}
|
|
|
|
{{=$data}} = {{=$coerced}};
|
|
|
|
{{? !$dataLvl }}if ({{=$parentData}} !== undefined){{?}}
|
|
|
|
{{=$parentData}}[{{=$parentDataProperty}}] = {{=$coerced}};
|
|
|
|
}
|
|
|
|
#}}
|