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