Skip to content

Commit e94eec3

Browse files
committed
add Lib.validate function:
- to determine if an attribute val is valid
1 parent b6fc59d commit e94eec3

File tree

3 files changed

+243
-0
lines changed

3 files changed

+243
-0
lines changed

src/lib/coerce.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,22 @@ exports.coerceFont = function(coerce, attr, dfltObj) {
309309

310310
return out;
311311
};
312+
313+
exports.validate = function(value, opts) {
314+
var valObject = exports.valObjects[opts.valType];
315+
316+
if(opts.arrayOk && Array.isArray(value)) return true;
317+
318+
if(valObject.validateFunction) {
319+
return valObject.validateFunction(value, opts);
320+
}
321+
322+
var failed = {},
323+
out = failed,
324+
propMock = { set: function(v) { out = v; } };
325+
326+
// 'failed' just something mutable that won't be === anything else
327+
328+
valObject.coerceFunction(value, propMock, failed, opts);
329+
return out !== failed;
330+
};

src/lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ lib.valObjects = coerceModule.valObjects;
2121
lib.coerce = coerceModule.coerce;
2222
lib.coerce2 = coerceModule.coerce2;
2323
lib.coerceFont = coerceModule.coerceFont;
24+
lib.validate = coerceModule.validate;
2425

2526
var datesModule = require('./dates');
2627
lib.dateTime2ms = datesModule.dateTime2ms;

test/jasmine/tests/lib_test.js

Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,229 @@ describe('Test lib.js:', function() {
755755
});
756756
});
757757

758+
fdescribe('validate', function() {
759+
760+
function assert(shouldPass, shouldFail, valObject) {
761+
shouldPass.forEach(function(v) {
762+
var res = Lib.validate(v, valObject);
763+
expect(res).toBe(true, JSON.stringify(v) + ' should pass');
764+
});
765+
766+
shouldFail.forEach(function(v) {
767+
var res = Lib.validate(v, valObject);
768+
expect(res).toBe(false, JSON.stringify(v) + ' should fail');
769+
});
770+
}
771+
772+
it('should work for valType \'data_array\' where', function() {
773+
var shouldPass = [[20], []],
774+
shouldFail = ['a', {}, 20, undefined, null];
775+
776+
assert(shouldPass, shouldFail, {
777+
valType: 'data_array'
778+
});
779+
780+
assert(shouldPass, shouldFail, {
781+
valType: 'data_array',
782+
dflt: [1, 2]
783+
});
784+
});
785+
786+
it('should work for valType \'enumerated\' where', function() {
787+
assert(['a', 'b'], ['c', 1, null, undefined, ''], {
788+
valType: 'enumerated',
789+
values: ['a', 'b'],
790+
dflt: 'a'
791+
});
792+
793+
assert([1, '1', 2, '2'], ['c', 3, null, undefined, ''], {
794+
valType: 'enumerated',
795+
values: [1, 2],
796+
coerceNumber: true,
797+
dflt: 1
798+
});
799+
800+
assert(['a', 'b', [1, 2]], ['c', 1, null, undefined, ''], {
801+
valType: 'enumerated',
802+
values: ['a', 'b'],
803+
arrayOk: true,
804+
dflt: 'a'
805+
});
806+
});
807+
808+
it('should work for valType \'boolean\' where', function() {
809+
var shouldPass = [true, false],
810+
shouldFail = ['a', 1, {}, [], null, undefined, ''];
811+
812+
assert(shouldPass, shouldFail, {
813+
valType: 'boolean',
814+
dflt: true
815+
});
816+
817+
assert(shouldPass, shouldFail, {
818+
valType: 'boolean',
819+
dflt: false
820+
});
821+
});
822+
823+
it('should work for valType \'number\' where', function() {
824+
var shouldPass = [20, '20', 1e6],
825+
shouldFail = ['a', [], {}, null, undefined, ''];
826+
827+
assert(shouldPass, shouldFail, {
828+
valType: 'number'
829+
});
830+
831+
assert(shouldPass, shouldFail, {
832+
valType: 'number',
833+
dflt: null
834+
});
835+
836+
assert([20, '20'], [-10, '-10', 25, '25'], {
837+
valType: 'number',
838+
dflt: 20,
839+
min: 0,
840+
max: 21
841+
});
842+
843+
assert([20, '20', [1, 2]], ['a', {}], {
844+
valType: 'number',
845+
dflt: 20,
846+
arrayOk: true
847+
});
848+
});
849+
850+
it('should work for valType \'integer\' where', function() {
851+
assert([1, 2, '3', '4'], ['a', 1.321321, {}, [], null, 2 / 3, undefined, null], {
852+
valType: 'integer',
853+
dflt: 1
854+
});
855+
856+
assert([1, 2, '3', '4'], [-1, '-2', 2.121, null, undefined, [], {}], {
857+
valType: 'integer',
858+
min: 0,
859+
dflt: 1
860+
});
861+
});
862+
863+
it('should work for valType \'string\' where', function() {
864+
865+
// This fails as the coerceFunction coerce all values
866+
// (except when 'strict' is true) using String()
867+
//
868+
// Things like undefined, null, {}, [] should be considered valid!
869+
//
870+
// The question becomes how the change this while not
871+
// scarifying perf??
872+
873+
assert(['3', '4', 'a'], [undefined, {}, [], null], {
874+
valType: 'string',
875+
dflt: 'a'
876+
});
877+
878+
assert(['3', '4', 'a'], [undefined, {}, [], null, ''], {
879+
valType: 'string',
880+
dflt: 'a',
881+
noBlank: true
882+
});
883+
884+
assert(['3', '4'], [undefined, 1, {}, [], null, ''], {
885+
valType: 'string',
886+
dflt: 'a',
887+
strict: true,
888+
noBlank: true
889+
});
890+
});
891+
892+
it('should work for valType \'color\' where', function() {
893+
var shouldPass = ['red', '#d3d3d3', 'rgba(0,255,255,0.1)'],
894+
shouldFail = [1, {}, [], 'rgq(233,122,332,1)', null, undefined];
895+
896+
assert(shouldPass, shouldFail, {
897+
valType: 'color'
898+
});
899+
});
900+
901+
it('should work for valType \'colorscale\' where', function() {
902+
var good = [ [0, 'red'], [1, 'blue'] ],
903+
bad = [ [0.1, 'red'], [1, 'blue'] ],
904+
bad2 = [ [0], [1] ],
905+
bad3 = [ ['red'], ['blue']],
906+
bad4 = ['red', 'blue'];
907+
908+
// Fails at [], should be an easy fix though.
909+
910+
var shouldPass = ['Viridis', 'Greens', good],
911+
shouldFail = ['red', 1, undefined, null, {}, [], bad, bad2, bad3, bad4];
912+
913+
assert(shouldPass, shouldFail, {
914+
valType: 'colorscale'
915+
});
916+
});
917+
918+
it('should work for valType \'angle\' where', function() {
919+
var shouldPass = ['auto', '120', 270],
920+
shouldFail = [{}, [], 'red', null, undefined, ''];
921+
922+
assert(shouldPass, shouldFail, {
923+
valType: 'angle',
924+
dflt: 0
925+
});
926+
});
927+
928+
it('should work for valType \'subplotid\' where', function() {
929+
var shouldPass = ['sp', 'sp1', 'sp4'],
930+
shouldFail = [{}, [], 'sp0', 'spee1', null, undefined];
931+
932+
// This fails because coerceFunction depends on dflt
933+
// having a length.
934+
//
935+
// Solution: don't use dflt as base string ->
936+
// add other options to valObject
937+
938+
assert(shouldPass, shouldFail, {
939+
valType: 'subplotid',
940+
dflt: 'sp'
941+
});
942+
});
943+
944+
it('should work for valType \'flaglist\' where', function() {
945+
var shouldPass = ['a', 'b', 'a+b', 'b+a', 'c'],
946+
shouldFail = [{}, [], 'red', null, undefined, '', 'a + b'];
947+
948+
assert(shouldPass, shouldFail, {
949+
valType: 'flaglist',
950+
flags: ['a', 'b'],
951+
extras: ['c']
952+
});
953+
});
954+
955+
it('should work for valType \'any\' where', function() {
956+
var shouldPass = ['', '120', null, false, {}, []],
957+
shouldFail = [undefined];
958+
959+
assert(shouldPass, shouldFail, {
960+
valType: 'any'
961+
});
962+
});
963+
964+
it('should work for valType \'info_array\' where', function() {
965+
var shouldPass = [[1, 2]],
966+
shouldFail = [{}, [], ['aads', null], 'red', null, undefined, ''];
967+
968+
// This fails. All array including [] are considered valid
969+
970+
assert(shouldPass, shouldFail, {
971+
valType: 'info_array',
972+
items: [{
973+
valType: 'number', dflt: -20
974+
}, {
975+
valType: 'number', dflt: 20
976+
}]
977+
});
978+
});
979+
});
980+
758981
describe('setCursor', function() {
759982

760983
beforeEach(function() {

0 commit comments

Comments
 (0)