update `==`, `object <-> array`
parent
ec09c3c735
commit
a21cf02f43
|
@ -4,8 +4,8 @@
|
||||||
, $coerced = 'coerced' + $lvl;
|
, $coerced = 'coerced' + $lvl;
|
||||||
}}
|
}}
|
||||||
var {{=$dataType}} = typeof {{=$data}};
|
var {{=$dataType}} = typeof {{=$data}};
|
||||||
{{? it.opts.coerceTypes === 'array'}}
|
{{? it.opts.coerceTypes == 'array'}}
|
||||||
if ({{=$dataType}} === 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array';
|
if ({{=$dataType}} == 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array';
|
||||||
{{?}}
|
{{?}}
|
||||||
|
|
||||||
var {{=$coerced}} = undefined;
|
var {{=$coerced}} = undefined;
|
||||||
|
@ -17,33 +17,33 @@
|
||||||
{{ $bracesCoercion += '}'; }}
|
{{ $bracesCoercion += '}'; }}
|
||||||
{{?}}
|
{{?}}
|
||||||
|
|
||||||
{{? it.opts.coerceTypes === 'array' && $type !== 'array' }}
|
{{? it.opts.coerceTypes == 'array' && $type != 'array' }}
|
||||||
if ({{=$dataType}} === 'array' && {{=$data}}.length === 1) {
|
if ({{=$dataType}} == 'array' && {{=$data}}.length == 1) {
|
||||||
{{=$coerced}} = {{=$data}} = {{=$data}}[0];
|
{{=$coerced}} = {{=$data}} = {{=$data}}[0];
|
||||||
{{=$dataType}} = typeof {{=$data}};
|
{{=$dataType}} = typeof {{=$data}};
|
||||||
/*if ({{=$dataType}} === 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array';*/
|
/*if ({{=$dataType}} == 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array';*/
|
||||||
}
|
}
|
||||||
{{?}}
|
{{?}}
|
||||||
|
|
||||||
{{? $type === 'string' }}
|
{{? $type == 'string' }}
|
||||||
if ({{=$dataType}} === 'number' || {{=$dataType}} === 'boolean')
|
if ({{=$dataType}} == 'number' || {{=$dataType}} == 'boolean')
|
||||||
{{=$coerced}} = '' + {{=$data}};
|
{{=$coerced}} = '' + {{=$data}};
|
||||||
else if ({{=$data}} === null) {{=$coerced}} = '';
|
else if ({{=$data}} === null) {{=$coerced}} = '';
|
||||||
{{?? $type === 'number' || $type === 'integer' }}
|
{{?? $type == 'number' || $type == 'integer' }}
|
||||||
if ({{=$dataType}} === 'boolean' || {{=$data}} === null
|
if ({{=$dataType}} == 'boolean' || {{=$data}} === null
|
||||||
|| ({{=$dataType}} === 'string' && {{=$data}} && {{=$data}} == +{{=$data}}
|
|| ({{=$dataType}} == 'string' && {{=$data}} && {{=$data}} == +{{=$data}}
|
||||||
{{? $type === 'integer' }} && !({{=$data}} % 1){{?}}))
|
{{? $type == 'integer' }} && !({{=$data}} % 1){{?}}))
|
||||||
{{=$coerced}} = +{{=$data}};
|
{{=$coerced}} = +{{=$data}};
|
||||||
{{?? $type === 'boolean' }}
|
{{?? $type == 'boolean' }}
|
||||||
if ({{=$data}} === 'false' || {{=$data}} === 0 || {{=$data}} === null)
|
if ({{=$data}} === 'false' || {{=$data}} === 0 || {{=$data}} === null)
|
||||||
{{=$coerced}} = false;
|
{{=$coerced}} = false;
|
||||||
else if ({{=$data}} === 'true' || {{=$data}} === 1)
|
else if ({{=$data}} === 'true' || {{=$data}} === 1)
|
||||||
{{=$coerced}} = true;
|
{{=$coerced}} = true;
|
||||||
{{?? $type === 'null' }}
|
{{?? $type == 'null' }}
|
||||||
if ({{=$data}} === '' || {{=$data}} === 0 || {{=$data}} === false)
|
if ({{=$data}} === '' || {{=$data}} === 0 || {{=$data}} === false)
|
||||||
{{=$coerced}} = null;
|
{{=$coerced}} = null;
|
||||||
{{?? it.opts.coerceTypes === 'array' && $type === 'array' }}
|
{{?? it.opts.coerceTypes == 'array' && $type == 'array' }}
|
||||||
if ({{=$dataType}} !== 'array')
|
if ({{=$dataType}} == 'string' || {{=$dataType}} == 'number' || {{=$dataType}} == 'boolean' || {{=$data}} == null)
|
||||||
{{=$coerced}} = [{{=$data}}];
|
{{=$coerced}} = [{{=$data}}];
|
||||||
{{?}}
|
{{?}}
|
||||||
{{~}}
|
{{~}}
|
||||||
|
|
|
@ -156,9 +156,9 @@ coercionArrayRules.boolean.array = [
|
||||||
coercionArrayRules.null.array = [
|
coercionArrayRules.null.array = [
|
||||||
{ from: [null], to: null }
|
{ from: [null], to: null }
|
||||||
];
|
];
|
||||||
/*coercionArrayRules.object.array = [
|
coercionArrayRules.object.array = [
|
||||||
{ from: [{}], to: {} }
|
{ from: [{}], to: undefined }
|
||||||
];*/
|
];
|
||||||
|
|
||||||
coercionArrayRules.array = {
|
coercionArrayRules.array = {
|
||||||
'string': [
|
'string': [
|
||||||
|
@ -174,7 +174,7 @@ coercionArrayRules.array = {
|
||||||
{from: null, to: [null]}
|
{from: null, to: [null]}
|
||||||
],
|
],
|
||||||
'object': [
|
'object': [
|
||||||
{from: {}, to: [{}]}
|
{from: {}, to: undefined}
|
||||||
]
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -206,7 +206,7 @@ describe('Type coercion', function () {
|
||||||
testRules(coercionArrayRules, function (test, schema, canCoerce, toType, fromType) {
|
testRules(coercionArrayRules, function (test, schema, canCoerce, toType, fromType) {
|
||||||
instances.forEach(function (ajv) {
|
instances.forEach(function (ajv) {
|
||||||
var valid = ajv.validate(schema, test.from);
|
var valid = ajv.validate(schema, test.from);
|
||||||
//if (valid !== canCoerce) console.log(toType, '.', fromType, test, schema, ajv.errors);
|
if (valid !== canCoerce) console.log(toType, '.', fromType, test, schema, ajv.errors);
|
||||||
valid. should.equal(canCoerce);
|
valid. should.equal(canCoerce);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue