source: imaps-frontend/node_modules/ajv/lib/dot/coerce.def

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.9 KB
Line 
1{{## def.coerceType:
2 {{
3 var $dataType = 'dataType' + $lvl
4 , $coerced = 'coerced' + $lvl;
5 }}
6 var {{=$dataType}} = typeof {{=$data}};
7 var {{=$coerced}} = undefined;
8
9 {{? it.opts.coerceTypes == 'array' }}
10 if ({{=$dataType}} == 'object' && Array.isArray({{=$data}}) && {{=$data}}.length == 1) {
11 {{=$data}} = {{=$data}}[0];
12 {{=$dataType}} = typeof {{=$data}};
13 if ({{=it.util.checkDataType(it.schema.type, $data, it.opts.strictNumbers)}}) {{=$coerced}} = {{=$data}};
14 }
15 {{?}}
16
17 if ({{=$coerced}} !== undefined) ;
18 {{~ $coerceToTypes:$type:$i }}
19 {{? $type == 'string' }}
20 else if ({{=$dataType}} == 'number' || {{=$dataType}} == 'boolean')
21 {{=$coerced}} = '' + {{=$data}};
22 else if ({{=$data}} === null) {{=$coerced}} = '';
23 {{?? $type == 'number' || $type == 'integer' }}
24 else if ({{=$dataType}} == 'boolean' || {{=$data}} === null
25 || ({{=$dataType}} == 'string' && {{=$data}} && {{=$data}} == +{{=$data}}
26 {{? $type == 'integer' }} && !({{=$data}} % 1){{?}}))
27 {{=$coerced}} = +{{=$data}};
28 {{?? $type == 'boolean' }}
29 else if ({{=$data}} === 'false' || {{=$data}} === 0 || {{=$data}} === null)
30 {{=$coerced}} = false;
31 else if ({{=$data}} === 'true' || {{=$data}} === 1)
32 {{=$coerced}} = true;
33 {{?? $type == 'null' }}
34 else if ({{=$data}} === '' || {{=$data}} === 0 || {{=$data}} === false)
35 {{=$coerced}} = null;
36 {{?? it.opts.coerceTypes == 'array' && $type == 'array' }}
37 else if ({{=$dataType}} == 'string' || {{=$dataType}} == 'number' || {{=$dataType}} == 'boolean' || {{=$data}} == null)
38 {{=$coerced}} = [{{=$data}}];
39 {{?}}
40 {{~}}
41 else {
42 {{# def.error:'type' }}
43 }
44
45 if ({{=$coerced}} !== undefined) {
46 {{# def.setParentData }}
47 {{=$data}} = {{=$coerced}};
48 {{? !$dataLvl }}if ({{=$parentData}} !== undefined){{?}}
49 {{=$parentData}}[{{=$parentDataProperty}}] = {{=$coerced}};
50 }
51#}}
Note: See TracBrowser for help on using the repository browser.