allow reversal and add more tests

master
will Farrell 2016-07-24 19:03:27 -06:00
parent 4d1aa5fe31
commit ec09c3c735
3 changed files with 117 additions and 86 deletions

View File

@ -11,8 +11,6 @@ Type coercion only happens if there is `type` keyword and if without coercion th
If there are multiple types allowed in `type` keyword the coercion will only happen if none of the types match the data and some of the scalar types are present (coercion to/from `object`/`array` is not possible). In this case the validating function will try coercing the data to each type in order until some of them succeeds.
If `coerceTypes` is set to `array`
Possible type coercions:
|from&nbsp;type&nbsp;&rarr;<br>to&nbsp;type&nbsp;&darr;|string|number|boolean|null|

View File

@ -4,6 +4,10 @@
, $coerced = 'coerced' + $lvl;
}}
var {{=$dataType}} = typeof {{=$data}};
{{? it.opts.coerceTypes === 'array'}}
if ({{=$dataType}} === 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array';
{{?}}
var {{=$coerced}} = undefined;
{{ var $bracesCoercion = ''; }}
@ -13,25 +17,32 @@
{{ $bracesCoercion += '}'; }}
{{?}}
{{? 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';*/
}
{{?}}
{{? $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;
{{?? $type === 'array' }}
{{?? it.opts.coerceTypes === 'array' && $type === 'array' }}
if ({{=$dataType}} !== 'array')
{{=$coerced}} = [{{=$data}}];
{{?}}

View File

@ -22,7 +22,8 @@ var coercionRules = {
{ from: {}, to: undefined }
],
'array': [
{ from: [], to: undefined }
{ from: [], to: undefined },
{ from: [1], to: undefined }
]
},
'number': {
@ -45,7 +46,8 @@ var coercionRules = {
{ from: {}, to: undefined }
],
'array': [
{ from: [], to: undefined }
{ from: [], to: undefined },
{ from: [true], to: undefined }
]
},
'integer': {
@ -68,7 +70,8 @@ var coercionRules = {
{ from: {}, to: undefined }
],
'array': [
{ from: [], to: undefined }
{ from: [], to: undefined },
{ from: ['1'], to: undefined }
]
},
'boolean': {
@ -76,7 +79,7 @@ var coercionRules = {
{ from: 'false', to: false },
{ from: 'true', to: true },
{ from: '', to: undefined },
{ from: 'abc', to: undefined },
{ from: 'abc', to: undefined }
],
'number': [
{ from: 0, to: false },
@ -91,7 +94,8 @@ var coercionRules = {
{ from: {}, to: undefined }
],
'array': [
{ from: [], to: undefined }
{ from: [], to: undefined },
{ from: [0], to: undefined }
]
},
'null': {
@ -112,7 +116,8 @@ var coercionRules = {
{ from: {}, to: undefined }
],
'array': [
{ from: [], to: undefined }
{ from: [], to: undefined },
{ from: [null], to: undefined }
]
},
'array': {
@ -135,8 +140,27 @@ var coercionRules = {
}
};
var coercionArrayRules = {
'array': {
var coercionArrayRules = JSON.parse(JSON.stringify(coercionRules));
coercionArrayRules.string.array = [
{ from: ['abc'], to: 'abc' }
];
coercionArrayRules.number.array = [
{ from: [1.5], to: 1.5 }
];
coercionArrayRules.integer.array = [
{ from: [1], to: 1 }
];
coercionArrayRules.boolean.array = [
{ from: [true], to: true }
];
coercionArrayRules.null.array = [
{ from: [null], to: null }
];
/*coercionArrayRules.object.array = [
{ from: [{}], to: {} }
];*/
coercionArrayRules.array = {
'string': [
{from: 'abc', to: ['abc']}
],
@ -152,7 +176,6 @@ var coercionArrayRules = {
'object': [
{from: {}, to: [{}]}
]
}
};
describe('Type coercion', function () {
@ -175,7 +198,7 @@ describe('Type coercion', function () {
});
});
it('should coerce scalar values in array', function() {
it('should coerce scalar values (coerceTypes = array)', function() {
ajv = new Ajv({ coerceTypes: 'array', verbose: true });
fullAjv = new Ajv({ coerceTypes: 'array', verbose: true, allErrors: true });
instances = [ ajv, fullAjv ];
@ -183,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('array', toType, fromType, test, ajv.errors);
//if (valid !== canCoerce) console.log(toType, '.', fromType, test, schema, ajv.errors);
valid. should.equal(canCoerce);
});
});
@ -366,7 +389,6 @@ describe('Type coercion', function () {
for (var toType in rules) {
for (var fromType in rules[toType]) {
var tests = rules[toType][fromType];
//if (toType === 'array') { console.log(toType, fromType, tests); }
tests.forEach(function (test) {
var canCoerce = test.to !== undefined;
var schema;