• R/O
  • SSH

micro-check: Commit

input validation library


Commit MetaInfo

Revisionaaf4b6096d64be95cd522f0fba52e5731628b1e5 (tree)
Time2017-04-14 14:55:03
Authorfrostbane <frostbane@prog...>
Commiterfrostbane

Log Message

Merge with release/0.0.1

Change Summary

Incremental Difference

diff -r 00d0c8b1107a -r aaf4b6096d64 Gruntfile.js
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Gruntfile.js Fri Apr 14 14:55:03 2017 +0900
@@ -0,0 +1,196 @@
1+/*
2+ * micro-check
3+ *
4+ *
5+ *
6+ */
7+
8+
9+module.exports = function(grunt){
10+
11+ 'use strict';
12+
13+ /* */// ------------------------------------------------ //
14+ /* */// 設定定義
15+ /* */// ------------------------------------------------ //
16+
17+ var jxUnitConfig = {
18+ test :{
19+ options :{
20+ stopOnFail :true,
21+ stopOnException :true,
22+ verbose :true,
23+ outputColor :true,
24+ debugMode :true,
25+ useLogIcons :true,
26+ },
27+ testsuites :[
28+ {
29+ target :"./src/microcheck.js",
30+ file :"./test/microcheck.test.js",
31+ },
32+ ],
33+ },
34+
35+ test_min :{
36+ options :{
37+ stopOnFail :true,
38+ stopOnException :true,
39+ verbose :false,
40+ outputColor :true,
41+ debugMode :false,
42+ useLogIcons :true,
43+ },
44+ testsuites :[
45+ {
46+ target :"./bin/microcheck/microcheck.js",
47+ file :"./test/microcheck.test.js",
48+ },
49+ ],
50+ },
51+
52+ },
53+
54+ jsDocConfig = {
55+ sable :{
56+ src :[
57+ './src/microcheck.js',
58+ //'./README.md',
59+ ],
60+ options :{
61+ destination :'./doc/microcheck',
62+ configure :"./jsdoc.conf.json",
63+ template :'./node_modules/jsdoc-sable'
64+ }
65+ },
66+ },
67+
68+ uglifyConfig = {
69+ release :{
70+ options :{
71+ preserveComments :function(node, comment){
72+ // preserve comments that start with a bang
73+ return /^!/.test(comment.value);
74+ },
75+ mangle :true,
76+ compress :true,
77+ },
78+
79+ files :{
80+ './bin/microcheck/microcheck.js' :[
81+ './src/microcheck.js',
82+ ],
83+ './bin/microcheck/microcheck.deprecated.js' :[
84+ './src/microcheck.deprecated.js',
85+ ],
86+ },
87+ },
88+ },
89+
90+ copyConfig = {
91+ release :{
92+ files :[
93+ {
94+ expand :false,
95+ src :[
96+ './LICENSE.md',
97+ ],
98+ dest :'./bin/microcheck/LICENSE.md',
99+ filter :'isFile'
100+ },
101+ {
102+ expand :false,
103+ src :[
104+ './package.json',
105+ ],
106+ dest :'./bin/microcheck/package.json',
107+ filter :'isFile'
108+ },
109+ ],
110+ },
111+ };
112+
113+ /* */// ------------------------------------------------ //
114+ /* */// 設定登録
115+ /* */// ------------------------------------------------ //
116+
117+ grunt.initConfig({
118+ jxunit :jxUnitConfig,
119+ jsdoc :jsDocConfig,
120+ uglify :uglifyConfig,
121+ copy :copyConfig,
122+
123+ });
124+
125+ /* */// ------------------------------------------------ //
126+ /* */// タスク読み込み
127+ /* */// ------------------------------------------------ //
128+
129+ [
130+ 'grunt-jxunit',
131+ 'grunt-jsdoc',
132+ 'grunt-contrib-uglify',
133+ 'grunt-contrib-copy',
134+ ]
135+ .map(function(task){
136+ grunt.loadNpmTasks(task);
137+ });
138+
139+ /* */// ------------------------------------------------ //
140+ /* */// タスク登録
141+ /* */// ------------------------------------------------ //
142+
143+ [
144+ {
145+ target :"test",
146+ commands :[
147+ "jxunit:test",
148+ ],
149+ },
150+ {
151+ summary :"test the minified source",
152+ target :"test_min",
153+ commands :[
154+ "jxunit:test_min",
155+ ],
156+ },
157+ {
158+ summary :"prepare the src dir for publish",
159+ target :"release",
160+ commands :[
161+ "uglify:release",
162+ "copy:release",
163+ "jsdoc:sable",
164+ "jxunit:test_min",
165+ ],
166+ },
167+ {
168+ summary :"document",
169+ target :"document:sable",
170+ alias :[
171+ "document",
172+ ],
173+ commands :[
174+ "jsdoc:sable",
175+ ],
176+ },
177+ ]
178+ .map(function(task){
179+ var alias = task.alias || [];
180+ if(alias.indexOf(task.name) < 0)
181+ alias = alias.concat([task.target]);
182+ alias.map(function(target){
183+ if(typeof target === typeof undefined ||
184+ target === null ||
185+ target.trim() === "")
186+ return;
187+
188+ grunt.registerTask(target, task.commands);
189+ });
190+
191+ });
192+
193+
194+ /* */// ------------------------------------------------ //
195+
196+};
diff -r 00d0c8b1107a -r aaf4b6096d64 LICENSE.md
--- a/LICENSE.md Thu Apr 13 14:52:21 2017 +0900
+++ b/LICENSE.md Fri Apr 14 14:55:03 2017 +0900
@@ -39,6 +39,6 @@
3939 notice, this list of conditions and the following disclaimer in the
4040 documentation and/or other materials provided with the distribution.
4141
42-3. Neither the name of micro-dialog nor the names of its
43-contributors may be used to endorse or promote products derived from
44-this software without specific prior written permission.
42+3. Neither the name micro-check nor the names of its contributors may be
43+used to endorse or promote products derived from this software without
44+specific prior written permission.
diff -r 00d0c8b1107a -r aaf4b6096d64 package.json
--- a/package.json Thu Apr 13 14:52:21 2017 +0900
+++ b/package.json Fri Apr 14 14:55:03 2017 +0900
@@ -1,12 +1,12 @@
11 {
22 "name" : "micro-check",
3- "version" : "v0.0.0",
3+ "version" : "v0.0.1",
44 "description" : "Object property validation",
55 "repository" : {
66 "type": "hg",
77 "url" : "https://bitbucket.org/frostbane/micro-check"
88 },
9- "main" : "./src/microcheck.js",
9+ "main" : "./microcheck.js",
1010 "license" : "SEE LICENSE IN LICENSE.md",
1111 "keywords" : [
1212 "micro-check",
diff -r 00d0c8b1107a -r aaf4b6096d64 src/checkmicro.js
--- a/src/checkmicro.js Thu Apr 13 14:52:21 2017 +0900
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,971 +0,0 @@
1-/*
2- * 2016.08.09
3- * frostbane
4- *
5- *
6- *
7- */
8-
9-// todo fix globals
10-/* global
11-isInt,
12-isFloat,
13-format,
14-getDecimalCount,
15-trim
16- */
17-
18-/* jshint
19-bitwise: false
20- */
21-
22-var sandbox08 = sandbox08 || {};
23-
24-
25-/**
26- * @name check
27- *
28- * @description
29- * <code>required, width, min, max, decimal</code> は任意。なかったら、
30- * そのチェックは行われない
31- *
32- * <p>name が空白のときにメッセージボックスが表示されません。
33- * メッセージボックス(sweetalert)の設定が__showError() の中にある
34- * (ハードコードされてる)</p>
35- *
36- * @function
37- *
38- * @param {String} name 空白の時にメッセージボックスがでない
39- * @param {String|Number} value チェックしたい値
40- * @param {Boolean} [required] 必須の項目かどうか、デフォールトは true
41- * @param {Number} [width] 桁数のチェックが必要のときに桁数にセットする
42- * @param {Number} [min] 最小値のチェックが必要のときに最小値にセットする
43- * @param {Number} [max] 最大値のチェックが必要のときに最大値にセットする
44- * @param {Number} [decimal] 少数点のチェックが必要のときに 0~ の値にセットする
45- * @param {Array} [include]
46- * @param {Array} [exclude]
47- *
48- * @example
49- * // 必須でない
50- * // 10 ~ 100 の範囲
51- * var result = check({
52- * name : "面積",
53- * value : "20", //文字列でも大丈夫
54- * required: false,
55- * min : "10", //文字列でも大丈夫
56- * max : "100", //文字列でも大丈夫
57- * });
58- * // result === true
59- *
60- * @example
61- * // 必須
62- * // 最大文字数が24文字
63- * var result = check({
64- * name : "目標",
65- * value : "毎日が金曜日だと思っておく。",
66- * width : 24,
67- * });
68- * // result === true
69- *
70- * @example
71- * // 必須ではない
72- * // 小数点 2桁まで
73- * // 最小値が 0.1
74- * var result = check({
75- * name : "目標",
76- * value : 1.23,
77- * decimal : 2,
78- * required: false,
79- * min : 0.1,
80- * });
81- * // result === true
82- *
83- * @example
84- * // whiteリスト
85- * // includeにない項目はだめ
86- * var result1 = check({
87- * name : "性別",
88- * value : "new half",
89- * include : ["m", "M", "f", "F"],
90- * });
91- * // result1 === false
92- *
93- * // blackリスト
94- * // exclude にある項目はだめ
95- * var result2 = check({
96- * name : "人生",
97- * value : "4",
98- * exclude : [4, 96, 42],
99- * });
100- * // result2 === false
101- *
102- * @example
103- * // 以下も可能だが、非推奨
104- * var name = "名称";
105- * var value = 1.234;
106- * var required = false;
107- * var width = 5;
108- * ...
109- *
110- *
111- * // 以下も可能。推奨ではない
112- * var result1 = check(name, value, required, width, ...);
113- * var result2 = check("項目2", 1.234, true, 4, 1, 10, 0);
114- * var result3 = check("項目3", 1, true, 1, 1, 10, 0);
115- * var result4 = check("項目4", 3, true, 4, 1, 10, 1, [1, 2, 3]);
116- *
117- * @returns {Boolean} b
118- *
119- */
120-var checkmicro_old = function(){
121- "use strict";
122-
123- var __showError = function(message){
124- console.error(message);
125- };
126-
127- var __hasNoValue = function(option){
128- return option.value === null || trim(option.value + "") === "";
129- };
130-
131- var __required = function(option){
132-
133- if(__hasNoValue(option)){
134- var message = format("{{ name }}は必須入力項目です。", option);
135-
136- return option.name !== "" ?
137- __showError(message) :
138- false;
139-
140- }
141-
142- return true;
143- };
144-
145- var __width = function(option){
146- var value = option.value + "";
147-
148- if(!isInt(option.width))
149- throw new Error("width が整数ではありません。");
150-
151- option.width = parseInt(option.width);
152-
153- if(value.length > option.width){
154- var message = format("{{ name }}が指定された文字数を超えています。\n" +
155- "※{{ name }}に入力可能な文字数は" +
156- "{{ width }}桁までです。",
157- option);
158-
159- return option.name !== "" ?
160- __showError(message) :
161- false;
162-
163- }
164-
165- return true;
166-
167- };
168-
169- var __number = function(option){
170- //option.__type = option.decimal !== 0 ?
171- // '数字' :
172- // '整数' ;
173-
174- var message = "";
175- if(!isFloat(option.value)){
176- message =
177- format('{{ name }}は数値のみ入力可能です。', option);
178-
179- return option.name !== "" ?
180- __showError(message) :
181- false;
182-
183- }
184-
185- return true;
186- };
187-
188- var __min = function(option){
189-
190- if(!__number(option))
191- return false;
192- //throw new Error("チェックしたい値が数字ではない。");
193-
194- option.min = option.decimal === 0 ?
195- parseInt(option.min) :
196- parseFloat(option.min);
197-
198- if(!isFloat(option.min))
199- throw new Error("min値が数字ではない。");
200-
201- option.value = option.decimal === 0 ?
202- parseInt(option.value) :
203- parseFloat(option.value);
204-
205- option._min = option.decimal <= 0 ?
206- option.min :
207- option.min.toFixed(option.decimal);
208-
209- if(option.value < option.min){
210- var message = format('{{ name }}の入力値は、{{ _min }}より' +
211- '大きな値を設定して下さい。', option);
212-
213- return option.name !== "" ?
214- __showError(message) :
215- false;
216- }
217-
218- return true;
219-
220- };
221-
222- var __max = function(option){
223- if(!__number(option))
224- return false;
225- //throw new Error("チェックしたい値が数字ではない。");
226-
227- option.max = option.decimal === 0 ?
228- parseInt(option.max) :
229- parseFloat(option.max);
230-
231- if(!isFloat(option.max))
232- throw new Error("max値が数字ではない。");
233-
234- option.value = option.decimal === 0 ?
235- parseInt(option.value) :
236- parseFloat(option.value);
237-
238- option._max = option.decimal <= 0 ?
239- option.max :
240- option.max.toFixed(option.decimal);
241-
242- if(option.value > option.max){
243- var message = format('{{ name }}の入力値は、{{ _max }}より' +
244- '小さな値を設定して下さい', option);
245-
246- return option.name !== "" ?
247- __showError(message) :
248- false;
249- }
250-
251- return true;
252-
253- };
254-
255- var __decimal = function(option){
256- if(!__number(option))
257- return false;
258- //throw new Error("チェックしたい値が数字ではない。");
259-
260- if(option.decimal >= 0){
261- var decimalCount = getDecimalCount(option.value);
262-
263- if(decimalCount > option.decimal){
264- var message = format('{{ name }}に入力可能な小数の桁数は、' +
265- '{{ decimal }}桁です。\n' +
266- '{{ name }}をご確認下さい。', option);
267-
268- return option.name !== "" ?
269- __showError(message) :
270- false;
271- }
272- }
273-
274- return true;
275- };
276-
277- var __include = function(option){
278- if(!(option.include instanceof Array))
279- throw new Error("include は配列ではない。");
280-
281- var floatVal = parseFloat(option.value);
282- var strngVal = option.value + "";
283-
284- if((!isFloat(option.value) ||
285- option.include.filter(function(x){
286- if(!isFloat(x))
287- return false;
288-
289- return parseFloat(x) === floatVal;
290- }).length === 0) &&
291- option.include.indexOf(strngVal) < 0){
292-
293- var list = option.include.map(function(inc){
294- return "'" + inc + "'";
295- });
296-
297- var message = format("{{ name }}が可能な値は{{ list }}に" +
298- "なります。入力した値を再度ご確認ください",
299- {
300- name :option.name,
301- list :list.join("か"),
302- });
303-
304- return option.name !== "" ?
305- __showError(message) :
306- false;
307- }
308-
309- return true;
310- };
311-
312- var __exclude = function(option){
313- if(!(option.exclude instanceof Array))
314- throw new Error("exclude は配列ではない。");
315-
316- var floatVal = parseFloat(option.value);
317- var strngVal = option.value + "";
318-
319- if((isFloat(option.value) &&
320- option.exclude.filter(function(x){
321- if(!isFloat(x))
322- return false;
323-
324- return parseFloat(x) === floatVal;
325- }).length !== 0) ||
326- option.exclude.indexOf(strngVal) >= 0){
327-
328- var list = option.exclude.map(function(inc){
329- return "'" + inc + "'";
330- });
331-
332- var message = format("{{ name }}が不可能な値は{{ list }}に" +
333- "なります。入力した値を再度ご確認ください",
334- {
335- name :option.name,
336- list :list.join("か"),
337- });
338-
339- return option.name !== "" ?
340- __showError(message) :
341- false;
342- }
343-
344- return true;
345-
346- };
347-
348- var __main = function(){
349- var option = Array.prototype.slice.call(arguments);
350- var defaults = {
351- name :"",
352- value :null,
353- min :null,
354- max :null,
355- width :null,
356- required :true,
357- decimal :null,
358- include :null,
359- exclude :null,
360- };
361-
362- if(option.length !== 1){
363- //kubota.research
364- // .common
365- // .check(name, value, required, width,
366- // min, max, decimal)
367-
368- // check(name, value ...) 非推奨
369- // check({name: ..., value: ...}) 推奨
370-
371- var tmpOption = {
372- name :option[0],
373- value :option[1],
374- required :option[2],
375- width :option[3],
376- min :option[4],
377- max :option[5],
378- decimal :option[6],
379- include :option[7],
380- exclude :option[8],
381- };
382-
383- option = tmpOption;
384-
385- }else{
386- option = option[0];
387- }
388-
389- var overwrite = overwrite;
390- option = overwrite(defaults).with(option);
391-
392- var result = true;
393-
394- if(result && option.required !== false)
395- result &= __required(option);
396-
397- if(__hasNoValue(option))
398- return result === true || result === 1;
399-
400- if(result && option.width !== null)
401- result &= __width(option);
402-
403- if(result && option.min !== null)
404- result &= __min(option);
405-
406- if(result && option.max !== null)
407- result &= __max(option);
408-
409- if(result && option.decimal !== null)
410- result &= __decimal(option);
411-
412- if(result && option.include !== null)
413- result &= __include(option);
414-
415- if(result && option.exclude !== null)
416- result &= __exclude(option);
417-
418- return result === true || result === 1;
419- };
420-
421- return __main.apply(null, arguments);
422-
423-};
424-
425-
426-if(typeof exports !== typeof undefined && this.exports !== exports)
427- module.exports = checkmicro_old;
428-
429-
430-var CheckMicro = function(validate){
431-
432- "use strict";
433-
434- var __options,
435- __object;
436-
437- var __ruleNames = [
438- //@see {Form_validation.php}
439- //@see {http://datamapper.maglok.nl/pages/validation.html}
440- "required",
441- "regex_match",
442- "matches",
443- "matches_exact",
444- //"is_unique",
445- //"unique",
446- //"unique_pair",
447- "min_length",
448- "max_length",
449- "exact_length",
450- "valid_email",
451- //"valid_emails",
452- //"valid_ip",
453- "alpha",
454- "alpha_numeric",
455- //"alpha_dash_dot",
456- //"alpha_slash_dot",
457- //"alpha_dash",
458- "numeric",
459- //"boolean",
460- //"is_numeric",
461- //"integer",
462- //"decimal",
463- //"greater_than",
464- //"less_than",
465- //"is_natural",
466- //"is_natural_no_zero",
467- //"valid_base64",
468- //"include",
469- //"exclude",
470- "min_value",
471- "max_value",
472- //"min_date",
473- //"max_date",
474- //"valid_date",
475- //"valid_date_group",
476- ];
477-
478- var __rules = {
479- required :function(a, b){
480- var getMessage = CheckMicro.message.required;
481-
482- if(b instanceof Array){
483- getMessage = b[1];
484- b = b[0];
485- }
486-
487- if(typeof a === typeof "")
488- a = a.trim();
489-
490- var hasVal = typeof a !== typeof undefined &&
491- a !== null &&
492- a !== "";
493-
494- return b ?
495- hasVal ?
496- false :
497- getMessage.call(this) :
498- false;
499-
500-
501- },
502-
503- regex_match :function(a, b){
504- var getMessage = CheckMicro.message.regex_match;
505-
506- if(b instanceof Array){
507- getMessage = b[1];
508- b = b[0];
509- }
510-
511- return new RegExp(b).test(a) ?
512- false :
513- getMessage.call(this);
514-
515- },
516-
517- matches :function(a, b){
518- var getMessage = CheckMicro.message.matches;
519-
520- /**! use ==
521- */
522- return a == b ?
523- false :
524- getMessage.call(this);
525- },
526-
527- matches_exact :function(a, b){
528- var getMessage = CheckMicro.message.matches_exact;
529-
530- return a === b ?
531- false :
532- getMessage.call(this);
533- },
534-
535- min_length :function(a, b){
536- var getMessage = CheckMicro.message.min_length;
537-
538- return a.length >= b ?
539- false :
540- getMessage.call(this);
541- },
542-
543- max_length :function(a, b){
544- var getMessage = CheckMicro.message.max_length;
545- return a.length <= b ?
546- false :
547- getMessage.call(this);
548- },
549-
550- exact_length :function(a, b){
551- var getMessage = CheckMicro.message.exact_length;
552-
553- return __rules.min_length.call(this, a, b) === false &&
554- __rules.max_length.call(this, a, b) === false ?
555- false:
556- getMessage.call(this);
557- },
558-
559- valid_email :function(a, b){
560- var getMessage = CheckMicro.message.valid_email;
561-
562- var isValid = new RegExp("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$").test(a);
563-
564- return b ?
565- isValid ?
566- false :
567- getMessage.call(this) :
568- false;
569- },
570-
571- alpha :function(a, b){
572- var getMessage = CheckMicro.message.alpha;
573-
574- var isAlphabet = new RegExp("^[a-zA-Z]$").test(a);
575-
576- return b ?
577- false :
578- getMessage.call(this);
579- },
580-
581- alpha_numeric :function(a, b){
582- var isAlphaNumeric = new RegExp("^[a-zA-Z0-9]$").test(a);
583-
584- return b ?
585- isAlphaNumeric :
586- !isAlphaNumeric;
587-
588- },
589-
590- numeric :function(a, b){
591- var getMessage = CheckMicro.message.numeric;
592-
593- var isNumeric = new RegExp("^[\-+]?[0-9]*$").test(a) &&
594- (a + "").split(".").length <= 2;
595-
596- return b ?
597- isNumeric ?
598- false :
599- getMessage.call(this, b) :
600- !isNumeric ?
601- false :
602- getMessage.call(this, b);
603-
604- },
605-
606- min_value :function(a, b){
607- return a >= b;
608- },
609-
610- max_value :function(a, b){
611- return a <= b;
612- },
613-
614- };
615-
616- var __extendRules = function(extension){
617- var typeofFunction = typeof function(){};
618-
619- for(var prop in extension){
620- if(!extension.hasOwnProperty(prop))
621- continue;
622-
623- if(typeof extension[prop] !== typeofFunction)
624- continue;
625-
626- __rules[prop] = extension[prop];
627- }
628-
629- };
630-
631- var __getAllArrays = function(obj, path){
632-
633- var objName = path.substr(0, path.indexOf("["));
634- //if(!obj.hasOwnProperty(objName))
635- // return undefined;
636-
637- var indexes = [];
638- var curPath = path;
639- do{
640- var start = curPath.indexOf("["),
641- end = curPath.indexOf("]"),
642- objIndex = curPath.substr(start + 1, end - start - 1);
643-
644- curPath = curPath.substr(end + 1);
645-
646- indexes.push(objIndex);
647-
648- }while(curPath !== "");
649-
650- var result = [];
651-
652- indexes.map(function(prop, index){
653- if(prop === "*"){
654- result = __getPropertiesArray(obj, objName);
655- return;
656- }
657-
658- result = __getArrayValueDefault(obj, objName, prop);
659-
660- });
661-
662- return result;
663-
664- };
665-
666- var __getPropertiesArray = function(obj, objName){
667- var result = [];
668-
669- obj.map(function(o){
670- o[objName].map(function(arr_o){
671- result.push(arr_o);
672-
673- });
674- });
675-
676- return result;
677-
678- };
679-
680- var __getAllProperties = function(obj){
681- var result = [];
682-
683- obj.map(function(o){
684- if(o === undefined){
685- result.push(o);
686-
687- return;
688- }
689-
690- for(var p in o){
691- if(o.hasOwnProperty(p)){
692- result.push(o[p]);
693-
694- }else{
695- result.push(undefined);
696-
697- }
698-
699- }
700-
701- });
702-
703- return result;
704-
705- };
706-
707- var __getValueDefault = function(obj, prop){
708- var result = [];
709-
710- obj.map(function(o){
711- if(o === undefined){
712- result.push(o);
713- return;
714- }
715-
716- if(!o.hasOwnProperty(prop)){
717- result.push(undefined);
718- return;
719- }
720-
721- result.push(o[prop]);
722-
723- });
724-
725- return result;
726-
727- };
728-
729- var __getArrayValueDefault = function(obj, objName, prop){
730- var result = [];
731-
732- obj.map(function(o){
733- if(o === undefined){
734- result.push(o);
735- return;
736- }
737-
738- if(!o.hasOwnProperty(objName)){
739- result.push(undefined);
740- return;
741- }
742-
743- if(!o[objName].hasOwnProperty(prop)){
744- result.push(undefined);
745- return;
746- }
747-
748- result.push(o[objName][prop]);
749-
750- });
751-
752- return result;
753-
754- };
755-
756- var __getValues = function(path){
757- var path_arr = path.split(".");
758-
759- var object = [
760- __object
761- ];
762-
763- path_arr.map(function(prop, index){
764- if(prop === "")
765- return;
766-
767- if(prop === "*"){
768- object = __getAllProperties(object);
769- return;
770- }
771-
772- if(prop.indexOf("[") >= 0){
773- object = __getAllArrays(object, prop);
774- return;
775- }
776-
777- object = __getValueDefault(object, prop);
778-
779- });
780-
781- return object;
782-
783- };
784-
785- var __validateRule = function(option, values){
786- var name = option[0],
787- path = option[1],
788- rules = option[2];
789-
790- var results = [];
791-
792- var isRequired = rules.hasOwnProperty("required") &&
793- rules.required === true;
794-
795- if(rules.hasOwnProperty("required")){
796- values.map(function(a, i){
797- var context = {
798- name :name,
799- path :path,
800- rules :rules,
801- rule :{
802- name :"required",
803- value :rules.required,
804- },
805- value :a,
806- };
807-
808- results.push(__rules.required.call(context, a, rules.required));
809- });
810- }
811-
812- var functionObject = typeof function(){};
813-
814- for(var item in rules){
815- if(!rules.hasOwnProperty(item))
816- continue;
817-
818- if(item === "required")
819- continue;
820-
821- if(typeof rules[item] !== functionObject && !__rules.hasOwnProperty(item)){
822- //no rule named item
823-
824- //kasa
825- continue;
826- }
827-
828- var f = typeof rules[item] === functionObject ?
829- rules[item] :
830- __rules[item];
831-
832- values.map(function(a, i){
833-
834- if(typeof a === typeof undefined ||
835- a === null ||
836- a === "")
837- return;
838-
839- var context = {
840- name :name,
841- path :path,
842- rules :rules,
843- rule :{
844- name :item,
845- value :rules[item],
846- },
847- value :a,
848- };
849-
850- results.push(f.call(context, a, rules[item]));
851- });
852-
853- }
854-
855- return results;
856- };
857-
858- var __validateOne = function(option){
859- var name = option[0],
860- path = option[1],
861- rules = option[2];
862-
863- var values = __getValues(path);
864-
865- return __validateRule(option, values);
866- };
867-
868- var __validateAll = function(object){
869- var groupResults = __options.map(__validateOne);
870-
871- return groupResults.reduce(function(current, result, index){
872- return current.concat(result);
873- }, [])
874- .filter(function(result){
875- return result !== false;
876- });
877- };
878-
879- var __setData = function(data){
880- __object = data;
881-
882- return this;
883- };
884-
885- var __initialize = function(options){
886- __options = options.filter(function(opt){
887- return typeof opt === typeof {} &&
888- opt instanceof Array &&
889- opt.length === 3 &&
890- typeof opt[0] === typeof "" &&
891- typeof opt[1] === typeof "" &&
892- typeof opt[2] === typeof {};
893- });
894-
895-
896- };
897-
898- __initialize(validate);
899-
900- return {
901- getRuleNames :function(){
902- return __ruleNames;
903- },
904- getRules :function(){
905- return __rules;
906- },
907- extendRules :__extendRules,
908- setData :__setData,
909- validate :__validateAll,
910- getValues :__getValues,
911- };
912-
913-};
914-
915-CheckMicro.message = {
916- required :function(){
917- "use strict";
918-
919- return '"' + this.name + '" is required';
920- },
921- regex_match :function(){
922- "use strict";
923-
924- return '"' + this.name + '" does not match the regular expression "' + this.rule.value + '"';
925- },
926- matches :function(){
927- "use strict";
928-
929- return '"' + this.name + '" does not match "' + this.rule.value + '"';
930-
931- },
932- matches_exact :function(){
933- "use strict";
934-
935- return '"' + this.name + '" does not match "' + this.rule.value + '"';
936-
937- },
938- min_length :function(){
939- "use strict";
940-
941- return '"' + this.name + '" has a minimum of "' + this.rule.value + '" characters';
942- },
943- max_length :function(){
944- "use strict";
945-
946- return '"' + this.name + '" has a maximum of "' + this.rule.value + '" characters';
947- },
948- exact_length :function(){
949- "use strict";
950-
951- return '"' + this.name + '" needs "' + this.rule.value + '" characters';
952- },
953- valid_email :function(){
954- "use strict";
955-
956- return '"' + this.value + '" is an invalid email';
957- },
958- alpha :function(){
959- "use strict";
960-
961- return '"' + this.value + '" is not alphanumeric';
962- },
963- numeric :function(b){
964- "use strict";
965-
966- return b ?
967- '"' + this.value + '" is not a number' :
968- '"' + this.value + '" is a number';
969- },
970-
971-};
diff -r 00d0c8b1107a -r aaf4b6096d64 src/microcheck.deprecated.js
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/microcheck.deprecated.js Fri Apr 14 14:55:03 2017 +0900
@@ -0,0 +1,465 @@
1+/*!
2+ .. ┌ -: .
3+ ([]▄▄├]▄▄▄¿┐`,
4+ .¡ ,. D{}▓███████████░█∩^} =
5+ ,▓¼ .▐▓▓█████████████▓██▓▓, (▓
6+ █▌╕. ─@╣████████████████████▄─ ./▀'
7+ :▓█▌Ç... └ '└▓██████████████████████Ö'` .. '└`'┌
8+ \ g█████▄Ü┌ C g██████████████████████████╗─ ² ² t▄▓██ù- `╞
9+ ¡ Å╠█▌╠█▓▓▓▓▌N@▌| ╓██████████████████████████▌ .╣▓▓▓▓▓▓▓]▐╙ÅÅ∩!
10+ ` ` ╫½┌▀▀▀▀▀▀░╫▌┘ ▀▀▀▀▀██████████████████▀█▀C :╝▀▀▀▀▀▀▀. `:
11+ L ─ⁿ'``' ─ ''' ─█████████████████▌─█= ╞º'`'º──
12+ ..Å╫█╛. ~
13+ ╔▄▄▓██▓▄¿ .
14+ - ]:├╣██▀▀▀▀▀▒Q¿.¿{ )=
15+ <─Ω╚█░▓███'──h⌂▓██╡██▌Ü─4
16+ `┌ ▐▓█▄▓██╟≤¡yQ▄███▓█▌▐▓M
17+ ^. └▀▀▄▓██▄▒▄▄╬▄████▀ └▀╙`
18+ «-'''█▓▓█▓██▓██▓▓██└``'⌐
19+ #╛ µ:▓▄;xφ(⌠ █
20+ ╗µ ~╙'` `┌█╣
21+ ╝Ñ. '' ' L▀▀
22+ ~ ,
23+ ^
24+ ÷ ─.
25+ N. ∩ts )
26+ ∩ ²⌠ ` ─
27+ (@ ~▒▒ .
28+ ` └▀/ .>▀▀ :
29+ ⌐ ,⌐
30+
31+
32+ Validation library
33+ Version 0.0.0
34+ Copyright (c) 2016, 2017 Frostbane Ac
35+ See LICENSE.md.
36+ www.??.com
37+
38+
39+ */
40+
41+ /*
42+ * 2016.08.09
43+ * frostbane
44+ *
45+ * Original validator made for KSI
46+ *
47+ */
48+
49+// todo fix globals
50+/* global
51+isInt,
52+isFloat,
53+format,
54+getDecimalCount,
55+trim
56+ */
57+
58+/* jshint
59+bitwise: false
60+ */
61+
62+
63+/**
64+ * @name check
65+ *
66+ * @description
67+ * <code>required, width, min, max, decimal</code> は任意。なかったら、
68+ * そのチェックは行われない
69+ *
70+ * <p>name が空白のときにメッセージボックスが表示されません。
71+ * メッセージボックス(sweetalert)の設定が__showError() の中にある
72+ * (ハードコードされてる)</p>
73+ *
74+ * @function
75+ *
76+ * @param {String} name 空白の時にメッセージボックスがでない
77+ * @param {String|Number} value チェックしたい値
78+ * @param {Boolean} [required] 必須の項目かどうか、デフォールトは true
79+ * @param {Number} [width] 桁数のチェックが必要のときに桁数にセットする
80+ * @param {Number} [min] 最小値のチェックが必要のときに最小値にセットする
81+ * @param {Number} [max] 最大値のチェックが必要のときに最大値にセットする
82+ * @param {Number} [decimal] 少数点のチェックが必要のときに 0~ の値にセットする
83+ * @param {Array} [include]
84+ * @param {Array} [exclude]
85+ *
86+ * @example
87+ * // 必須でない
88+ * // 10 ~ 100 の範囲
89+ * var result = check({
90+ * name : "面積",
91+ * value : "20", //文字列でも大丈夫
92+ * required: false,
93+ * min : "10", //文字列でも大丈夫
94+ * max : "100", //文字列でも大丈夫
95+ * });
96+ * // result === true
97+ *
98+ * @example
99+ * // 必須
100+ * // 最大文字数が24文字
101+ * var result = check({
102+ * name : "目標",
103+ * value : "毎日が金曜日だと思っておく。",
104+ * width : 24,
105+ * });
106+ * // result === true
107+ *
108+ * @example
109+ * // 必須ではない
110+ * // 小数点 2桁まで
111+ * // 最小値が 0.1
112+ * var result = check({
113+ * name : "目標",
114+ * value : 1.23,
115+ * decimal : 2,
116+ * required: false,
117+ * min : 0.1,
118+ * });
119+ * // result === true
120+ *
121+ * @example
122+ * // whiteリスト
123+ * // includeにない項目はだめ
124+ * var result1 = check({
125+ * name : "性別",
126+ * value : "new half",
127+ * include : ["m", "M", "f", "F"],
128+ * });
129+ * // result1 === false
130+ *
131+ * // blackリスト
132+ * // exclude にある項目はだめ
133+ * var result2 = check({
134+ * name : "人生",
135+ * value : "4",
136+ * exclude : [4, 96, 42],
137+ * });
138+ * // result2 === false
139+ *
140+ * @example
141+ * // 以下も可能だが、非推奨
142+ * var name = "名称";
143+ * var value = 1.234;
144+ * var required = false;
145+ * var width = 5;
146+ * ...
147+ *
148+ *
149+ * // 以下も可能。推奨ではない
150+ * var result1 = check(name, value, required, width, ...);
151+ * var result2 = check("項目2", 1.234, true, 4, 1, 10, 0);
152+ * var result3 = check("項目3", 1, true, 1, 1, 10, 0);
153+ * var result4 = check("項目4", 3, true, 4, 1, 10, 1, [1, 2, 3]);
154+ *
155+ * @returns {Boolean} b
156+ *
157+ */
158+var checkmicro_old = function(){
159+ "use strict";
160+
161+ var __showError = function(message){
162+ console.error(message);
163+ };
164+
165+ var __hasNoValue = function(option){
166+ return option.value === null || trim(option.value + "") === "";
167+ };
168+
169+ var __required = function(option){
170+
171+ if(__hasNoValue(option)){
172+ var message = format("{{ name }}は必須入力項目です。", option);
173+
174+ return option.name !== "" ?
175+ __showError(message) :
176+ false;
177+
178+ }
179+
180+ return true;
181+ };
182+
183+ var __width = function(option){
184+ var value = option.value + "";
185+
186+ if(!isInt(option.width))
187+ throw new Error("width が整数ではありません。");
188+
189+ option.width = parseInt(option.width);
190+
191+ if(value.length > option.width){
192+ var message = format("{{ name }}が指定された文字数を超えています。\n" +
193+ "※{{ name }}に入力可能な文字数は" +
194+ "{{ width }}桁までです。",
195+ option);
196+
197+ return option.name !== "" ?
198+ __showError(message) :
199+ false;
200+
201+ }
202+
203+ return true;
204+
205+ };
206+
207+ var __number = function(option){
208+ //option.__type = option.decimal !== 0 ?
209+ // '数字' :
210+ // '整数' ;
211+
212+ var message = "";
213+ if(!isFloat(option.value)){
214+ message =
215+ format('{{ name }}は数値のみ入力可能です。', option);
216+
217+ return option.name !== "" ?
218+ __showError(message) :
219+ false;
220+
221+ }
222+
223+ return true;
224+ };
225+
226+ var __min = function(option){
227+
228+ if(!__number(option))
229+ return false;
230+ //throw new Error("チェックしたい値が数字ではない。");
231+
232+ option.min = option.decimal === 0 ?
233+ parseInt(option.min) :
234+ parseFloat(option.min);
235+
236+ if(!isFloat(option.min))
237+ throw new Error("min値が数字ではない。");
238+
239+ option.value = option.decimal === 0 ?
240+ parseInt(option.value) :
241+ parseFloat(option.value);
242+
243+ option._min = option.decimal <= 0 ?
244+ option.min :
245+ option.min.toFixed(option.decimal);
246+
247+ if(option.value < option.min){
248+ var message = format('{{ name }}の入力値は、{{ _min }}より' +
249+ '大きな値を設定して下さい。', option);
250+
251+ return option.name !== "" ?
252+ __showError(message) :
253+ false;
254+ }
255+
256+ return true;
257+
258+ };
259+
260+ var __max = function(option){
261+ if(!__number(option))
262+ return false;
263+ //throw new Error("チェックしたい値が数字ではない。");
264+
265+ option.max = option.decimal === 0 ?
266+ parseInt(option.max) :
267+ parseFloat(option.max);
268+
269+ if(!isFloat(option.max))
270+ throw new Error("max値が数字ではない。");
271+
272+ option.value = option.decimal === 0 ?
273+ parseInt(option.value) :
274+ parseFloat(option.value);
275+
276+ option._max = option.decimal <= 0 ?
277+ option.max :
278+ option.max.toFixed(option.decimal);
279+
280+ if(option.value > option.max){
281+ var message = format('{{ name }}の入力値は、{{ _max }}より' +
282+ '小さな値を設定して下さい', option);
283+
284+ return option.name !== "" ?
285+ __showError(message) :
286+ false;
287+ }
288+
289+ return true;
290+
291+ };
292+
293+ var __decimal = function(option){
294+ if(!__number(option))
295+ return false;
296+ //throw new Error("チェックしたい値が数字ではない。");
297+
298+ if(option.decimal >= 0){
299+ var decimalCount = getDecimalCount(option.value);
300+
301+ if(decimalCount > option.decimal){
302+ var message = format('{{ name }}に入力可能な小数の桁数は、' +
303+ '{{ decimal }}桁です。\n' +
304+ '{{ name }}をご確認下さい。', option);
305+
306+ return option.name !== "" ?
307+ __showError(message) :
308+ false;
309+ }
310+ }
311+
312+ return true;
313+ };
314+
315+ var __include = function(option){
316+ if(!(option.include instanceof Array))
317+ throw new Error("include は配列ではない。");
318+
319+ var floatVal = parseFloat(option.value);
320+ var strngVal = option.value + "";
321+
322+ if((!isFloat(option.value) ||
323+ option.include.filter(function(x){
324+ if(!isFloat(x))
325+ return false;
326+
327+ return parseFloat(x) === floatVal;
328+ }).length === 0) &&
329+ option.include.indexOf(strngVal) < 0){
330+
331+ var list = option.include.map(function(inc){
332+ return "'" + inc + "'";
333+ });
334+
335+ var message = format("{{ name }}が可能な値は{{ list }}に" +
336+ "なります。入力した値を再度ご確認ください",
337+ {
338+ name :option.name,
339+ list :list.join("か"),
340+ });
341+
342+ return option.name !== "" ?
343+ __showError(message) :
344+ false;
345+ }
346+
347+ return true;
348+ };
349+
350+ var __exclude = function(option){
351+ if(!(option.exclude instanceof Array))
352+ throw new Error("exclude は配列ではない。");
353+
354+ var floatVal = parseFloat(option.value);
355+ var strngVal = option.value + "";
356+
357+ if((isFloat(option.value) &&
358+ option.exclude.filter(function(x){
359+ if(!isFloat(x))
360+ return false;
361+
362+ return parseFloat(x) === floatVal;
363+ }).length !== 0) ||
364+ option.exclude.indexOf(strngVal) >= 0){
365+
366+ var list = option.exclude.map(function(inc){
367+ return "'" + inc + "'";
368+ });
369+
370+ var message = format("{{ name }}が不可能な値は{{ list }}に" +
371+ "なります。入力した値を再度ご確認ください",
372+ {
373+ name :option.name,
374+ list :list.join("か"),
375+ });
376+
377+ return option.name !== "" ?
378+ __showError(message) :
379+ false;
380+ }
381+
382+ return true;
383+
384+ };
385+
386+ var __main = function(){
387+ var option = Array.prototype.slice.call(arguments);
388+ var defaults = {
389+ name :"",
390+ value :null,
391+ min :null,
392+ max :null,
393+ width :null,
394+ required :true,
395+ decimal :null,
396+ include :null,
397+ exclude :null,
398+ };
399+
400+ if(option.length !== 1){
401+ //kubota.research
402+ // .common
403+ // .check(name, value, required, width,
404+ // min, max, decimal)
405+
406+ // check(name, value ...) 非推奨
407+ // check({name: ..., value: ...}) 推奨
408+
409+ var tmpOption = {
410+ name :option[0],
411+ value :option[1],
412+ required :option[2],
413+ width :option[3],
414+ min :option[4],
415+ max :option[5],
416+ decimal :option[6],
417+ include :option[7],
418+ exclude :option[8],
419+ };
420+
421+ option = tmpOption;
422+
423+ }else{
424+ option = option[0];
425+ }
426+
427+ var overwrite = overwrite;
428+ option = overwrite(defaults).with(option);
429+
430+ var result = true;
431+
432+ if(result && option.required !== false)
433+ result &= __required(option);
434+
435+ if(__hasNoValue(option))
436+ return result === true || result === 1;
437+
438+ if(result && option.width !== null)
439+ result &= __width(option);
440+
441+ if(result && option.min !== null)
442+ result &= __min(option);
443+
444+ if(result && option.max !== null)
445+ result &= __max(option);
446+
447+ if(result && option.decimal !== null)
448+ result &= __decimal(option);
449+
450+ if(result && option.include !== null)
451+ result &= __include(option);
452+
453+ if(result && option.exclude !== null)
454+ result &= __exclude(option);
455+
456+ return result === true || result === 1;
457+ };
458+
459+ return __main.apply(null, arguments);
460+
461+};
462+
463+
464+if(typeof exports !== typeof undefined && this.exports !== exports)
465+ module.exports = checkmicro_old;
diff -r 00d0c8b1107a -r aaf4b6096d64 src/microcheck.js
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/microcheck.js Fri Apr 14 14:55:03 2017 +0900
@@ -0,0 +1,654 @@
1+/*!
2+ .. ┌ -: .
3+ ([]▄▄├]▄▄▄¿┐`,
4+ .¡ ,. D{}▓███████████░█∩^} =
5+ ,▓¼ .▐▓▓█████████████▓██▓▓, (▓
6+ █▌╕. ─@╣████████████████████▄─ ./▀'
7+ :▓█▌Ç... └ '└▓██████████████████████Ö'` .. '└`'┌
8+ \ g█████▄Ü┌ C g██████████████████████████╗─ ² ² t▄▓██ù- `╞
9+ ¡ Å╠█▌╠█▓▓▓▓▌N@▌| ╓██████████████████████████▌ .╣▓▓▓▓▓▓▓]▐╙ÅÅ∩!
10+ ` ` ╫½┌▀▀▀▀▀▀░╫▌┘ ▀▀▀▀▀██████████████████▀█▀C :╝▀▀▀▀▀▀▀. `:
11+ L ─ⁿ'``' ─ ''' ─█████████████████▌─█= ╞º'`'º──
12+ ..Å╫█╛. ~
13+ ╔▄▄▓██▓▄¿ .
14+ - ]:├╣██▀▀▀▀▀▒Q¿.¿{ )=
15+ <─Ω╚█░▓███'──h⌂▓██╡██▌Ü─4
16+ `┌ ▐▓█▄▓██╟≤¡yQ▄███▓█▌▐▓M
17+ ^. └▀▀▄▓██▄▒▄▄╬▄████▀ └▀╙`
18+ «-'''█▓▓█▓██▓██▓▓██└``'⌐
19+ #╛ µ:▓▄;xφ(⌠ █
20+ ╗µ ~╙'` `┌█╣
21+ ╝Ñ. '' ' L▀▀
22+ ~ ,
23+ ^
24+ ÷ ─.
25+ N. ∩ts )
26+ ∩ ²⌠ ` ─
27+ (@ ~▒▒ .
28+ ` └▀/ .>▀▀ :
29+ ⌐ ,⌐
30+
31+
32+ Object property validation
33+ Version 0.0.1
34+ Copyright (c) 2016, 2017 Frostbane Ac
35+ See LICENSE.md.
36+ www.??.com
37+
38+
39+ */
40+
41+ /*
42+ * 2016.08.12
43+ * frostbane
44+ *
45+ *
46+ *
47+ */
48+
49+// todo fix globals
50+/* global
51+isInt,
52+isFloat,
53+format,
54+getDecimalCount,
55+trim
56+ */
57+
58+/* jshint
59+bitwise: false
60+ */
61+
62+(function(global, factory){
63+ "use strict";
64+
65+ if(typeof exports !== typeof undefined && global.exports !== exports){
66+ module.exports = factory;
67+ }else{
68+ global.MicroCheck = factory;
69+ }
70+
71+}(this, (function(){
72+ "use strict";
73+
74+ // todo move all regular expressions here so that all will be
75+ // instatiated only once
76+
77+ var MicroCheck = function(validate){
78+
79+ "use strict";
80+
81+ var __options,
82+ __object;
83+
84+ var __ruleNames = [
85+ //@see {Form_validation.php}
86+ //@see {http://datamapper.maglok.nl/pages/validation.html}
87+ "required",
88+ "regex_match",
89+ "matches",
90+ "matches_exact",
91+ //"is_unique",
92+ //"unique",
93+ //"unique_pair",
94+ "min_length",
95+ "max_length",
96+ "exact_length",
97+ "valid_email",
98+ //"valid_emails",
99+ //"valid_ip",
100+ "alpha",
101+ "alpha_numeric",
102+ //"alpha_dash_dot",
103+ //"alpha_slash_dot",
104+ //"alpha_dash",
105+ "numeric",
106+ //"boolean",
107+ //"is_numeric",
108+ //"integer",
109+ //"decimal",
110+ //"greater_than",
111+ //"less_than",
112+ //"is_natural",
113+ //"is_natural_no_zero",
114+ //"valid_base64",
115+ //"include",
116+ //"exclude",
117+ "min_value",
118+ "max_value",
119+ //"min_date",
120+ //"max_date",
121+ //"valid_date",
122+ //"valid_date_group",
123+ "single_byte",
124+ ];
125+
126+ var __rules = {
127+ required :function(a, b){
128+ var getMessage = MicroCheck.message.required;
129+
130+ if(b instanceof Array){
131+ getMessage = b[1];
132+ b = b[0];
133+ }
134+
135+ if(typeof a === typeof "")
136+ a = a.trim();
137+
138+ var hasVal = typeof a !== typeof undefined &&
139+ a !== null &&
140+ a !== "";
141+
142+ return b ?
143+ hasVal ?
144+ false :
145+ getMessage.call(this) :
146+ false;
147+
148+
149+ },
150+
151+ regex_match :function(a, b){
152+ var getMessage = MicroCheck.message.regex_match;
153+
154+ if(b instanceof Array){
155+ getMessage = b[1];
156+ b = b[0];
157+ }
158+
159+ var re = b instanceof RegExp ?
160+ b:
161+ new RegExp(b);
162+
163+ return re.test(a) ?
164+ false :
165+ getMessage.call(this);
166+
167+ },
168+
169+ matches :function(a, b){
170+ var getMessage = MicroCheck.message.matches;
171+
172+ /**! use ==
173+ */
174+ return a == b ?
175+ false :
176+ getMessage.call(this);
177+ },
178+
179+ matches_exact :function(a, b){
180+ var getMessage = MicroCheck.message.matches_exact;
181+
182+ return a === b ?
183+ false :
184+ getMessage.call(this);
185+ },
186+
187+ min_length :function(a, b){
188+ var getMessage = MicroCheck.message.min_length;
189+
190+ return a.length >= b ?
191+ false :
192+ getMessage.call(this);
193+ },
194+
195+ max_length :function(a, b){
196+ var getMessage = MicroCheck.message.max_length;
197+ return a.length <= b ?
198+ false :
199+ getMessage.call(this);
200+ },
201+
202+ exact_length :function(a, b){
203+ var getMessage = MicroCheck.message.exact_length;
204+
205+ return __rules.min_length.call(this, a, b) === false &&
206+ __rules.max_length.call(this, a, b) === false ?
207+ false:
208+ getMessage.call(this);
209+ },
210+
211+ valid_email :function(a, b){
212+ var getMessage = MicroCheck.message.valid_email;
213+
214+ var isValid = new RegExp("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$").test(a);
215+
216+ return b ?
217+ isValid ?
218+ false :
219+ getMessage.call(this) :
220+ false;
221+ },
222+
223+ alpha :function(a, b){
224+ var getMessage = MicroCheck.message.alpha;
225+
226+ var isAlphabet = new RegExp("^[a-zA-Z]*$").test(a);
227+
228+ return b ?
229+ isAlphabet ?
230+ false :
231+ getMessage.call(this):
232+ false;
233+ },
234+
235+ alpha_numeric :function(a, b){
236+ var getMessage = MicroCheck.message.alpha_numeric;
237+
238+ var isAlphaNumeric = new RegExp("^[a-zA-Z0-9]*$").test(a);
239+
240+ return b ?
241+ isAlphaNumeric ?
242+ false :
243+ getMessage.call(this) :
244+ false;
245+
246+ },
247+
248+ numeric :function(a, b){
249+ var getMessage = MicroCheck.message.numeric;
250+
251+ var isNumeric = new RegExp("^[\-+]?[0-9]*$").test(a) &&
252+ (a + "").split(".").length <= 2;
253+
254+ return b ?
255+ isNumeric ?
256+ false :
257+ getMessage.call(this, b) :
258+ !isNumeric ?
259+ false :
260+ getMessage.call(this, b);
261+
262+ },
263+
264+ min_value :function(a, b){
265+ return a >= b;
266+ },
267+
268+ max_value :function(a, b){
269+ return a <= b;
270+ },
271+
272+ single_byte :function(a, b){
273+ var getMessage = MicroCheck.message.single_byte;
274+
275+ var isSingle = new RegExp(/[^\u0000-\u00ff]/).test(a);
276+
277+ return b ?
278+ !isSingle ?
279+ false :
280+ getMessage.call(this) :
281+ false;
282+
283+ },
284+
285+ };
286+
287+ var __extendRules = function(extension){
288+ var typeofFunction = typeof function(){};
289+
290+ for(var prop in extension){
291+ if(!extension.hasOwnProperty(prop))
292+ continue;
293+
294+ if(typeof extension[prop] !== typeofFunction)
295+ continue;
296+
297+ __rules[prop] = extension[prop];
298+ }
299+
300+ };
301+
302+ var __getAllArrays = function(obj, path){
303+
304+ var objName = path.substr(0, path.indexOf("["));
305+ //if(!obj.hasOwnProperty(objName))
306+ // return undefined;
307+
308+ var indexes = [];
309+ var curPath = path;
310+ do{
311+ var start = curPath.indexOf("["),
312+ end = curPath.indexOf("]"),
313+ objIndex = curPath.substr(start + 1, end - start - 1);
314+
315+ curPath = curPath.substr(end + 1);
316+
317+ indexes.push(objIndex);
318+
319+ }while(curPath !== "");
320+
321+ var result = [];
322+
323+ indexes.map(function(prop, index){
324+ if(prop === "*"){
325+ result = __getPropertiesArray(obj, objName);
326+ return;
327+ }
328+
329+ result = __getArrayValueDefault(obj, objName, prop);
330+
331+ });
332+
333+ return result;
334+
335+ };
336+
337+ var __getPropertiesArray = function(obj, objName){
338+ var result = [];
339+
340+ obj.map(function(o){
341+ o[objName].map(function(arr_o){
342+ result.push(arr_o);
343+
344+ });
345+ });
346+
347+ return result;
348+
349+ };
350+
351+ var __getAllProperties = function(obj){
352+ var result = [];
353+
354+ obj.map(function(o){
355+ if(o === undefined){
356+ result.push(o);
357+
358+ return;
359+ }
360+
361+ for(var p in o){
362+ if(o.hasOwnProperty(p)){
363+ result.push(o[p]);
364+
365+ }else{
366+ result.push(undefined);
367+
368+ }
369+
370+ }
371+
372+ });
373+
374+ return result;
375+
376+ };
377+
378+ var __getValueDefault = function(obj, prop){
379+ var result = [];
380+
381+ obj.map(function(o){
382+ if(o === undefined){
383+ result.push(o);
384+ return;
385+ }
386+
387+ if(!o.hasOwnProperty(prop)){
388+ result.push(undefined);
389+ return;
390+ }
391+
392+ result.push(o[prop]);
393+
394+ });
395+
396+ return result;
397+
398+ };
399+
400+ var __getArrayValueDefault = function(obj, objName, prop){
401+ var result = [];
402+
403+ obj.map(function(o){
404+ if(o === undefined){
405+ result.push(o);
406+ return;
407+ }
408+
409+ if(!o.hasOwnProperty(objName)){
410+ result.push(undefined);
411+ return;
412+ }
413+
414+ if(!o[objName].hasOwnProperty(prop)){
415+ result.push(undefined);
416+ return;
417+ }
418+
419+ result.push(o[objName][prop]);
420+
421+ });
422+
423+ return result;
424+
425+ };
426+
427+ var __getValues = function(path){
428+ var path_arr = path.split(".");
429+
430+ var object = [
431+ __object
432+ ];
433+
434+ path_arr.map(function(prop, index){
435+ if(prop === "")
436+ return;
437+
438+ if(prop === "*"){
439+ object = __getAllProperties(object);
440+ return;
441+ }
442+
443+ if(prop.indexOf("[") >= 0){
444+ object = __getAllArrays(object, prop);
445+ return;
446+ }
447+
448+ object = __getValueDefault(object, prop);
449+
450+ });
451+
452+ return object;
453+
454+ };
455+
456+ var __validateRule = function(option, values){
457+ var name = option[0],
458+ path = option[1],
459+ rules = option[2];
460+
461+ var results = [];
462+
463+ var isRequired = rules.hasOwnProperty("required") &&
464+ rules.required === true;
465+
466+ if(rules.hasOwnProperty("required")){
467+ values.map(function(a, i){
468+ var context = {
469+ name :name,
470+ path :path,
471+ rules :rules,
472+ rule :{
473+ name :"required",
474+ value :rules.required,
475+ },
476+ value :a,
477+ };
478+
479+ results.push(__rules.required.call(context, a, rules.required));
480+ });
481+ }
482+
483+ var functionObject = typeof function(){};
484+
485+ for(var item in rules){
486+ if(!rules.hasOwnProperty(item))
487+ continue;
488+
489+ if(item === "required")
490+ continue;
491+
492+ if(typeof rules[item] !== functionObject && !__rules.hasOwnProperty(item)){
493+ //no rule named item
494+
495+ //kasa
496+ continue;
497+ }
498+
499+ var f = typeof rules[item] === functionObject ?
500+ rules[item] :
501+ __rules[item];
502+
503+ values.map(function(a, i){
504+
505+ if(typeof a === typeof undefined ||
506+ a === null ||
507+ a === "")
508+ return;
509+
510+ var context = {
511+ name :name,
512+ path :path,
513+ rules :rules,
514+ rule :{
515+ name :item,
516+ value :rules[item],
517+ },
518+ value :a,
519+ };
520+
521+ results.push(f.call(context, a, rules[item]));
522+ });
523+
524+ }
525+
526+ return results;
527+ };
528+
529+ var __validateOne = function(option){
530+ var name = option[0],
531+ path = option[1],
532+ rules = option[2];
533+
534+ var values = __getValues(path);
535+
536+ return __validateRule(option, values);
537+ };
538+
539+ var __validateAll = function(object){
540+ var groupResults = __options.map(__validateOne);
541+
542+ return groupResults.reduce(function(current, result, index){
543+ return current.concat(result);
544+ }, [])
545+ .filter(function(result){
546+ return result !== false;
547+ });
548+ };
549+
550+ var __setData = function(data){
551+ __object = data;
552+
553+ return this;
554+ };
555+
556+ var __initialize = function(options){
557+ __options = options.filter(function(opt){
558+ return typeof opt === typeof {} &&
559+ opt instanceof Array &&
560+ opt.length === 3 &&
561+ typeof opt[0] === typeof "" &&
562+ typeof opt[1] === typeof "" &&
563+ typeof opt[2] === typeof {};
564+ });
565+
566+
567+ };
568+
569+ __initialize(validate);
570+
571+ return {
572+ getRuleNames :function(){
573+ return __ruleNames;
574+ },
575+ getRules :function(){
576+ return __rules;
577+ },
578+ extendRules :__extendRules,
579+ setData :__setData,
580+ validate :__validateAll,
581+ getValues :__getValues,
582+ };
583+
584+ };
585+
586+ MicroCheck.message = {
587+ required :function(){
588+ "use strict";
589+
590+ return '"' + this.name + '" is required';
591+ },
592+ regex_match :function(){
593+ "use strict";
594+
595+ return '"' + this.name + '" does not match the regular expression "' + this.rule.value + '"';
596+ },
597+ matches :function(){
598+ "use strict";
599+
600+ return '"' + this.name + '" does not match "' + this.rule.value + '"';
601+
602+ },
603+ matches_exact :function(){
604+ "use strict";
605+
606+ return '"' + this.name + '" does not match "' + this.rule.value + '"';
607+
608+ },
609+ min_length :function(){
610+ "use strict";
611+
612+ return '"' + this.name + '" has a minimum of "' + this.rule.value + '" characters';
613+ },
614+ max_length :function(){
615+ "use strict";
616+
617+ return '"' + this.name + '" has a maximum of "' + this.rule.value + '" characters';
618+ },
619+ exact_length :function(){
620+ "use strict";
621+
622+ return '"' + this.name + '" needs "' + this.rule.value + '" characters';
623+ },
624+ valid_email :function(){
625+ "use strict";
626+
627+ return '"' + this.value + '" is an invalid email';
628+ },
629+ alpha :function(){
630+ "use strict";
631+
632+ return '"' + this.value + '" contains non-alphabetical characters';
633+ },
634+ alpha_numeric :function(){
635+ "use strict";
636+
637+ return '"' + this.value + '" contains non-alphanumeric characters';
638+ },
639+ numeric :function(b){
640+ "use strict";
641+
642+ return b ?
643+ '"' + this.value + '" is not a number' :
644+ '"' + this.value + '" is a number';
645+ },
646+ single_byte :function(){
647+ return '"' + this.value + '" contains unicode characters';
648+ },
649+
650+ };
651+
652+ return MicroCheck;
653+
654+}())));
diff -r 00d0c8b1107a -r aaf4b6096d64 test/checkmicro.test.html
--- a/test/checkmicro.test.html Thu Apr 13 14:52:21 2017 +0900
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
1-<!DOCTYPE html>
2-
3-<html>
4-<head>
5- <meta charset="utf-8" />
6-
7- <script type="text/javascript"
8- src="../node_modules/jxunit/lib/jxunit.js"></script>
9- <script type="text/javascript"
10- src="../src/checkmicro.js"></script>
11- <script type="text/javascript"
12- src="checkmicro.test.js"></script>
13- <script type="text/javascript">
14- var Test = JxUnit.extend(checkmicrotest);
15-
16- var test = new Test({
17- clearOnStart :true,
18- stopOnFail :false,
19- stopOnException :true,
20- verbose :true,
21- autoStart :false,
22- outputColor :true,
23- debugMode :false,
24- useLogIcons :true,
25- });
26-
27- test.run();
28-
29- </script>
30-
31-
32-</head>
33-<body>
34-
35-
36-</body>
37-</html>
diff -r 00d0c8b1107a -r aaf4b6096d64 test/checkmicro.test.js
--- a/test/checkmicro.test.js Thu Apr 13 14:52:21 2017 +0900
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,877 +0,0 @@
1-/* jshint
2-maxlen: false
3- */
4-
5-/* global
6-log,
7-warn,
8-error,
9-mark,
10-assertTrue,
11-assertFalse,
12-assertEqual,
13-assertNotEqual,
14-assertUndefined,
15-assertNotUndefined,
16-assertSame,
17-assertNotSame,
18-result,
19-CheckMicro
20- */
21-
22-
23-/**
24- * @augments JxUnit
25- */
26-var checkmicrotest = {
27-
28-
29- /* */// ------------------------------------------------ //
30- /* */// variables
31- /* */// ------------------------------------------------ //
32-
33- data :{
34- companies :[
35- {
36- name :"axas",
37- id :1,
38- info :{
39- address :"西梅田",
40- members :[
41- "細川",
42- "岡澤",
43- ],
44- duration :1,
45- },
46- projects :[
47- {
48- name :"weekly report",
49- language :"php",
50- framework :"codeigniter",
51- },
52- {
53- name :"shoemert",
54- language :"php",
55- framework :"symfony",
56- },
57- {
58- name :"datamapper",
59- language :"php",
60- },
61- {
62- name :"addendum",
63- language :"php",
64- },
65- {
66- name :"checkmicro",
67- language :"javascript",
68- },
69- {
70- name :"ractive require",
71- language :"javascript",
72- },
73- {
74- name :"skeleton ex",
75- language :"css",
76- },
77- ],
78- },
79- {
80- name :"kds",
81- id :2,
82- info :{
83- address :"本町",
84- members :[
85- "岩崎圭介",
86- "井口まさふむ",
87- "尾崎",
88- "中塚",
89- "大森",
90- ],
91- duration :0.5,
92- },
93- projects :[
94- {
95- name :"toridoll",
96- language :"c#",
97- framework :"asp.net mvc",
98- },
99- {
100- name :"murata bht",
101- language :"javascript",
102- },
103- ],
104- },
105- {
106- name :"ksi",
107- id :3,
108- info :{
109- address :"大国町",
110- members :[
111- "平井阿弓",
112- "小林",
113- "牛込美恵",
114- "藤田隆三",
115- "武笠峻大",
116- "武笠峻大",
117- "小田安希子",
118- "藤原寛昌",
119- ],
120- },
121- projects :[
122- {
123- name :"vegetalia",
124- language :"c#",
125- phases :[
126- "basic design",
127- "function design",
128- "development",
129- "unit test",
130- ],
131- },
132- {
133- name :"engineer support web",
134- language :"c#",
135- framework :"asp.net mvc",
136- },
137- {
138- name :"log export",
139- language :"java",
140- framework :"struts",
141- },
142- {
143- name :"vehicle data replication",
144- language :"java",
145- },
146- {
147- name :"",
148- language :"",
149- },
150- ],
151- },
152- ],
153- employees :[
154- {
155- name :"ac",
156- id :1,
157- company :2,
158- info :{
159- address :"吹田",
160- members :[
161- "ケントハウス",
162- "ディレイ",
163- "ロングシャン",
164- ],
165- },
166- languages :[
167- "assembly",
168- "c",
169- "c++",
170- "c#",
171- "d",
172- "python",
173- "php",
174- "javascript",
175- "java",
176- "css",
177- "html",
178- "verilog",
179- "vhdl",
180- ],
181- email :"ac@mail.com",
182- type :"pro",
183- },
184- {
185- name :"ayumi",
186- id :2,
187- company :2,
188- info :{
189- address :"みなみ",
190- members :[
191- "べっぴん",
192- "うん",
193- ],
194- },
195- languages :[
196- "php",
197- "javascript",
198- "java",
199- "css",
200- "html",
201- ],
202- email :"naisho.da.yo",
203- type :null,
204- },
205- {
206- name :"akiko",
207- id :3,
208- company :3,
209- info :{
210- address :"そのへん",
211- members :[
212- "ちび",
213- ],
214- },
215- languages :[
216- "java",
217- "css",
218- "html",
219- ],
220- email :"akiko.oda@mail.co.jp",
221- type :undefined,
222- },
223- ],
224- next :{
225- company :{
226- name :"",
227- id :null,
228- },
229- employee :{
230- id :undefined,
231- },
232- language :{
233- name :null,
234- },
235- girl :{
236- name :undefined,
237- },
238- },
239- },
240-
241- /* */// ------------------------------------------------ //
242- /* */// startUp, setUp
243- /* */// ------------------------------------------------ //
244-
245- startUp :function(){
246- "use strict";
247-
248- },
249-
250- setUp :function(){
251- "use strict";
252-
253- },
254-
255- /* */// ------------------------------------------------ //
256- /* */// test(s)
257- /* */// ------------------------------------------------ //
258-
259- test_getSimpleValue :function(){
260- "use strict";
261-
262- var cm = new CheckMicro([])
263- .setData(this.data);
264-
265- // -----------------------------------------------------------
266- {
267- var result1 = cm.getValues("companies[1].projects[0].name");
268- var expected1 = this.data.companies[1].projects[0].name;
269- assertEqual(1, result1.length);
270- assertEqual(expected1, result1[0]);
271- }
272-
273- // -----------------------------------------------------------
274- {
275- var result2 = cm.getValues("companies[1].info.members[0]");
276- var expected2 = this.data.companies[1].info.members[0];
277- assertEqual(expected2, result2);
278- }
279-
280- },
281-
282- test_getBlobArrayValue :function(){
283- "use strict";
284-
285- var cm = new CheckMicro([])
286- .setData(this.data);
287-
288- // -----------------------------------------------------------
289- {
290- var result1 = cm.getValues("companies[*].projects[0].name");
291- var expected1 = this.data.companies.map(function(c){
292- return c.projects[0].name;
293- });
294- assertEqual(expected1.length, result1.length);
295- expected1.map(function(expected, index){
296- assertEqual(expected, result1[index]);
297- });
298- }
299-
300- // -----------------------------------------------------------
301- {
302- var result2 = cm.getValues("companies[1].info.members[*]");
303- var expected2 = this.data.companies[1].info.members;
304- assertEqual(expected2.length, result2.length);
305- expected2.map(function(expected, index){
306- assertEqual(expected, result2[index]);
307- });
308- }
309-
310- },
311-
312- test_getBlobPropertyValue :function(){
313- "use strict";
314-
315- var cm = new CheckMicro([])
316- .setData(this.data);
317-
318- // -----------------------------------------------------------
319- {
320- var result1 = cm.getValues("companies.*.name");
321- //log(result1);
322- }
323-
324- {
325- var result2 = cm.getValues("companies.*.info.members");
326- //log(result2);
327- }
328-
329-
330- },
331-
332- /* */// ------------------------------------------------ //
333-
334- test_matches :function(){
335- "use strict";
336-
337- var validationRules = [
338- [
339- "id of first company",
340- "companies[0].id",
341- {
342- matches :"1",
343- },
344- ],
345- [
346- "id of 2nd company",
347- "companies[1].id",
348- {
349- matches :2,
350- },
351- ],
352- ];
353-
354- var cm = new CheckMicro(validationRules)
355- .setData(this.data);
356-
357- var result = cm.validate();
358-
359- // no errors
360- assertEqual(0, result.length);
361-
362- },
363-
364- test_matches_fail :function(){
365- "use strict";
366-
367- var validationRules = [
368- [
369- "id of first company",
370- "companies[0].id",
371- {
372- matches :"11",
373- },
374- ],
375- [
376- "id of 2nd company",
377- "companies[1].id",
378- {
379- matches :22,
380- },
381- ],
382- ];
383-
384- var cm = new CheckMicro(validationRules)
385- .setData(this.data);
386-
387- var result = cm.validate();
388-
389- assertEqual(2, result.length);
390-
391- result.map(function(r){
392- log(r);
393- });
394-
395- },
396-
397- test_match_exact :function(){
398- "use strict";
399-
400- var validationRules = [
401- [
402- "first project of first company",
403- "companies[1].projects[0].name",
404- {
405- matches_exact :this.data.companies[1].projects[0].name,
406- },
407- ],
408- ];
409-
410- var cm = new CheckMicro(validationRules)
411- .setData(this.data);
412-
413- var result = cm.validate();
414-
415- assertEqual(0, result.length);
416-
417- },
418-
419- test_match_exact_fail :function(){
420- "use strict";
421-
422- var validationRules = [
423- [
424- "first project of first company",
425- "companies[1].projects[0].name",
426- {
427- matches_exact :"basic design",
428- },
429- ],
430- ];
431-
432- var cm = new CheckMicro(validationRules)
433- .setData(this.data);
434-
435- var result = cm.validate();
436-
437- assertEqual(1, result.length);
438-
439- result.map(function(r){
440- log(r);
441- });
442-
443- },
444-
445- test_required :function(){
446- "use strict";
447-
448- var validationRules = [
449- [
450- "company id",
451- "companies[*].id",
452- {
453- required :true,
454- },
455- ],
456- ];
457-
458- var cm = new CheckMicro(validationRules)
459- .setData(this.data);
460-
461- var result = cm.validate();
462-
463- assertEqual(0, result.length);
464-
465- },
466-
467- test_required_null :function(){
468- "use strict";
469-
470- var validationRules = [
471- [
472- "employees type",
473- "employees[*].type",
474- {
475- required :true,
476- },
477- ],
478- ];
479-
480- var cm = new CheckMicro(validationRules)
481- .setData(this.data);
482-
483- var result = cm.validate();
484-
485- assertEqual(2, result.length);
486-
487- result.map(function(r){
488- log(r);
489- });
490-
491- },
492-
493- test_not_required :function(){
494- "use strict";
495-
496- var validationRules = [
497- [
498- "next company",
499- "next.*.name",
500- {
501- required :false,
502- },
503- ],
504- ];
505-
506- var cm = new CheckMicro(validationRules)
507- .setData(this.data);
508-
509- var result = cm.validate();
510-
511- // all are not required so no validation is done
512- assertEqual(0, result.length);
513-
514- },
515-
516- test_required_fail :function(){
517- "use strict";
518-
519- var validationRules = [
520- [
521- "duration in company",
522- "companies[*].info.duration",
523- {
524- required :true,
525- },
526- ],
527- ];
528-
529- var cm = new CheckMicro(validationRules)
530- .setData(this.data);
531-
532- var result = cm.validate();
533-
534- // there are 4 next.*.name
535- assertEqual(1, result.length);
536-
537- result.map(function(r){
538- log(r);
539- });
540-
541- },
542-
543- test_max_length :function(){
544- "use strict";
545-
546- var validationRules = [
547- [
548- "company name",
549- "companies[*].name",
550- {
551- max_length :6,
552- },
553- ],
554- ];
555-
556- var cm = new CheckMicro(validationRules)
557- .setData(this.data);
558-
559- var result = cm.validate();
560-
561- // all test passed
562- assertEqual(0, result.length);
563-
564- },
565-
566- test_max_length_fail :function(){
567- "use strict";
568-
569- var validationRules = [
570- [
571- "company name",
572- "companies[*].name",
573- {
574- max_length :2,
575- },
576- ],
577- ];
578-
579- var cm = new CheckMicro(validationRules)
580- .setData(this.data);
581-
582- var result = cm.validate();
583-
584- // companies with names longer than 3
585- assertEqual(3, result.length);
586-
587- result.map(function(r){
588- log(r);
589- });
590-
591- },
592-
593- test_min_length :function(){
594- "use strict";
595-
596- var validationRules = [
597- [
598- "company name",
599- "companies[*].name",
600- {
601- min_length :2,
602- },
603- ],
604- ];
605-
606- var cm = new CheckMicro(validationRules)
607- .setData(this.data);
608-
609- var result = cm.validate();
610-
611- // all test passed
612- assertEqual(0, result.length);
613-
614- },
615-
616- test_min_length_fail :function(){
617- "use strict";
618-
619- var validationRules = [
620- [
621- "company name",
622- "companies[*].name",
623- {
624- min_length :10,
625- },
626- ],
627- ];
628-
629- var cm = new CheckMicro(validationRules)
630- .setData(this.data);
631-
632- var result = cm.validate();
633-
634- assertEqual(3, result.length);
635-
636- result.map(function(r){
637- log(r);
638- });
639-
640- },
641-
642- test_exact_length :function(){
643- "use strict";
644-
645- var validationRules = [
646- [
647- "company name",
648- "companies[0].name",
649- {
650- exact_length :4,
651- },
652- ],
653- ];
654-
655- var cm = new CheckMicro(validationRules)
656- .setData(this.data);
657-
658- var result = cm.validate();
659-
660- // all test passed
661- assertEqual(0, result.length);
662-
663- },
664-
665- test_exact_length_fail :function(){
666- "use strict";
667-
668- var validationRules = [
669- [
670- "company name",
671- "companies[0].name",
672- {
673- exact_length :10,
674- },
675- ],
676- ];
677-
678- var cm = new CheckMicro(validationRules)
679- .setData(this.data);
680-
681- var result = cm.validate();
682-
683- assertEqual(1, result.length);
684-
685- result.map(function(r){
686- log(r);
687- });
688-
689- },
690-
691- test_valid_email :function(){
692- "use strict";
693-
694- var validationRules = [
695- [
696- "ac's email",
697- "employees[0].email",
698- {
699- valid_email :true,
700- },
701- ],
702- ];
703-
704- var cm = new CheckMicro(validationRules)
705- .setData(this.data);
706-
707- var result = cm.validate();
708-
709- // all test passed
710- assertEqual(0, result.length);
711-
712- },
713-
714- test_invalid_email :function(){
715- "use strict";
716-
717- var validationRules = [
718- [
719- "aya's email",
720- "employees[1].email",
721- {
722- valid_email :false,
723- },
724- ],
725- ];
726-
727- var cm = new CheckMicro(validationRules)
728- .setData(this.data);
729-
730- var result = cm.validate();
731-
732- // all test passed
733- assertEqual(0, result.length);
734-
735- },
736-
737- test_valid_email_fail :function(){
738- "use strict";
739-
740- var validationRules = [
741- [
742- "ac's email",
743- "employees[*].email",
744- {
745- valid_email :true,
746- },
747- ],
748- ];
749-
750- var cm = new CheckMicro(validationRules)
751- .setData(this.data);
752-
753- var result = cm.validate();
754-
755- assertEqual(1, result.length);
756-
757- result.map(function(r){
758- log(r);
759- });
760-
761- },
762-
763- test_numeric :function(){
764- "use strict";
765-
766- var validationRules = [
767- [
768- "company id",
769- "companies[*].id",
770- {
771- numeric :true,
772- },
773- ],
774- ];
775-
776- var cm = new CheckMicro(validationRules)
777- .setData(this.data);
778-
779- var result = cm.validate();
780-
781- // all test passed
782- assertEqual(0, result.length);
783-
784- },
785-
786- test_numeric_fail :function(){
787- "use strict";
788-
789- var validationRules = [
790- [
791- "company project names",
792- "companies[*].projects[*].name",
793- {
794- numeric :true,
795- },
796- ],
797- ];
798-
799- var cm = new CheckMicro(validationRules)
800- .setData(this.data);
801-
802- var result = cm.validate();
803-
804- // all test passed
805- assertEqual(13, result.length);
806-
807- result.map(function(r){
808- log(r);
809- });
810-
811- },
812-
813- test_not_numeric :function(){
814- "use strict";
815-
816- var validationRules = [
817- [
818- "company name",
819- "companies[*].name",
820- {
821- numeric :false,
822- },
823- ],
824- ];
825-
826- var cm = new CheckMicro(validationRules)
827- .setData(this.data);
828-
829- var result = cm.validate();
830-
831- // all test passed
832- assertEqual(0, result.length);
833-
834- },
835-
836- test_not_numeric_fail :function(){
837- "use strict";
838-
839- var validationRules = [
840- [
841- "company id",
842- "companies[*].id",
843- {
844- numeric :false,
845- },
846- ],
847- ];
848-
849- var cm = new CheckMicro(validationRules)
850- .setData(this.data);
851-
852- var result = cm.validate();
853-
854- // all test passed
855- assertEqual(3, result.length);
856-
857- result.map(function(r){
858- log(r);
859- });
860-
861- },
862-
863- /* */// ------------------------------------------------ //
864- /* */// tearDown, shutDown
865- /* */// ------------------------------------------------ //
866-
867- tearDown :function(){
868-
869- },
870-
871- shutDown :function(){
872- "use strict";
873-
874- result.log();
875- },
876-
877-};
diff -r 00d0c8b1107a -r aaf4b6096d64 test/microcheck.test.html
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/microcheck.test.html Fri Apr 14 14:55:03 2017 +0900
@@ -0,0 +1,38 @@
1+<!DOCTYPE html>
2+
3+<html>
4+<head>
5+ <meta charset="utf-8" />
6+
7+ <script type="text/javascript"
8+ src="../node_modules/jxunit/lib/jxunit.js"></script>
9+ <script type="text/javascript"
10+ src="../src/microcheck.js"></script>
11+ <script type="text/javascript"
12+ src="microcheck.test.js"></script>
13+ <script type="text/javascript">
14+ var Test = JxUnit.extend(checkmicrotest);
15+
16+ var test = new Test({
17+ clearOnStart :true,
18+ stopOnFail :false,
19+ stopOnException :true,
20+ verbose :true,
21+ autoStart :false,
22+ outputColor :true,
23+ debugMode :false,
24+ useLogIcons :true,
25+ });
26+
27+ test.run();
28+
29+ </script>
30+
31+
32+</head>
33+<body>
34+ <h3>micro-check test runner</h3>
35+ <p>open console to see the results</p>
36+
37+</body>
38+</html>
diff -r 00d0c8b1107a -r aaf4b6096d64 test/microcheck.test.js
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/microcheck.test.js Fri Apr 14 14:55:03 2017 +0900
@@ -0,0 +1,1212 @@
1+/* jshint
2+maxlen: false
3+ */
4+
5+/* global
6+log,
7+warn,
8+error,
9+mark,
10+assertTrue,
11+assertFalse,
12+assertEqual,
13+assertNotEqual,
14+assertUndefined,
15+assertNotUndefined,
16+assertSame,
17+assertNotSame,
18+result,
19+MicroCheck
20+ */
21+
22+
23+/**
24+ * @augments JxUnit
25+ */
26+var checkmicrotest = {
27+
28+
29+ /* */// ------------------------------------------------ //
30+ /* */// variables
31+ /* */// ------------------------------------------------ //
32+
33+ data :{
34+ companies :[
35+ {
36+ name :"axas",
37+ code :"C00001X-001",
38+ id :1,
39+ info :{
40+ address :"西梅田",
41+ members :[
42+ "細川",
43+ "岡澤",
44+ ],
45+ duration :1,
46+ },
47+ projects :[
48+ {
49+ name :"weekly report",
50+ language :"php",
51+ framework :"codeigniter",
52+ },
53+ {
54+ name :"shoemert",
55+ language :"php",
56+ framework :"symfony",
57+ },
58+ {
59+ name :"datamapper",
60+ language :"php",
61+ },
62+ {
63+ name :"addendum",
64+ language :"php",
65+ },
66+ {
67+ name :"checkmicro",
68+ language :"javascript",
69+ },
70+ {
71+ name :"ractive require",
72+ language :"javascript",
73+ },
74+ {
75+ name :"skeleton ex",
76+ language :"css",
77+ },
78+ ],
79+ },
80+ {
81+ name :"kds",
82+ code :"C00002C",
83+ id :2,
84+ info :{
85+ address :"本町",
86+ members :[
87+ "岩崎圭介",
88+ "井口まさふむ",
89+ "尾崎",
90+ "中塚",
91+ "大森",
92+ ],
93+ duration :0.5,
94+ },
95+ projects :[
96+ {
97+ name :"toridoll",
98+ language :"c#",
99+ framework :"asp.net mvc",
100+ },
101+ {
102+ name :"murata bht",
103+ language :"javascript",
104+ },
105+ ],
106+ },
107+ {
108+ name :"ksi",
109+ code :"C00003F",
110+ id :3,
111+ info :{
112+ address :"大国町",
113+ members :[
114+ "平井阿弓",
115+ "小林",
116+ "牛込美恵",
117+ "藤田隆三",
118+ "武笠峻大",
119+ "武笠峻大",
120+ "小田安希子",
121+ "藤原寛昌",
122+ ],
123+ duration :0.3,
124+ },
125+ projects :[
126+ {
127+ name :"vegetalia",
128+ language :"c#",
129+ phases :[
130+ "basic design",
131+ "function design",
132+ "development",
133+ "unit test",
134+ ],
135+ },
136+ {
137+ name :"engineer support web",
138+ language :"c#",
139+ framework :"asp.net mvc",
140+ },
141+ {
142+ name :"log export",
143+ language :"java",
144+ framework :"struts",
145+ },
146+ {
147+ name :"vehicle data replication",
148+ language :"java",
149+ },
150+ {
151+ name :"",
152+ language :"",
153+ },
154+ ],
155+ },
156+ {
157+ name :"ogi",
158+ code :"C00004C",
159+ id :4,
160+ info :{
161+ address :"梅田",
162+ members :[
163+ "友廣哲也",
164+ "梅舎直紀",
165+ "榎本光貴",
166+ ],
167+ },
168+ projects :[
169+ {
170+ name :"medicaroid",
171+ language :"c#",
172+ phases :[
173+ "development",
174+ "unit test",
175+ ],
176+ framework :"aras",
177+ },
178+ {
179+ name :"robocad",
180+ language :"c#",
181+ framework :"aras",
182+ },
183+ {
184+ name :"export tool",
185+ language :"C#",
186+ framework :"windows forms",
187+ },
188+ ],
189+ },
190+ ],
191+ employees :[
192+ {
193+ name :"ac",
194+ id :1,
195+ company :2,
196+ info :{
197+ address :"吹田",
198+ members :[
199+ "ケントハウス",
200+ "ディレイ",
201+ "ロングシャン",
202+ ],
203+ },
204+ languages :[
205+ "assembly",
206+ "c",
207+ "c++",
208+ "c#",
209+ "d",
210+ "python",
211+ "php",
212+ "javascript",
213+ "java",
214+ "css",
215+ "html",
216+ "verilog",
217+ "vhdl",
218+ ],
219+ email :"ac@mail.com",
220+ type :"pro",
221+ },
222+ {
223+ name :"ayumi",
224+ id :2,
225+ company :2,
226+ info :{
227+ address :"みなみ",
228+ members :[
229+ "べっぴん",
230+ "うん",
231+ ],
232+ },
233+ languages :[
234+ "php",
235+ "javascript",
236+ "java",
237+ "css",
238+ "html",
239+ ],
240+ email :"naisho.da.yo",
241+ type :null,
242+ },
243+ {
244+ name :"akiko",
245+ id :3,
246+ company :3,
247+ info :{
248+ address :"そのへん",
249+ members :[
250+ "ちび",
251+ ],
252+ },
253+ languages :[
254+ "java",
255+ "css",
256+ "html",
257+ ],
258+ email :"akiko.oda@mail.co.jp",
259+ type :undefined,
260+ },
261+ ],
262+ next :{
263+ company :{
264+ name :"",
265+ code :"X00000X",
266+ id :null,
267+ },
268+ employee :{
269+ id :undefined,
270+ },
271+ language :{
272+ name :null,
273+ },
274+ girl :{
275+ name :undefined,
276+ },
277+ },
278+ },
279+
280+ /* */// ------------------------------------------------ //
281+ /* */// startUp, setUp
282+ /* */// ------------------------------------------------ //
283+
284+ startUp :function(){
285+ "use strict";
286+
287+ },
288+
289+ setUp :function(){
290+ "use strict";
291+
292+ },
293+
294+ /* */// ------------------------------------------------ //
295+ /* */// test(s)
296+ /* */// ------------------------------------------------ //
297+
298+ test_getSimpleValue :function(){
299+ "use strict";
300+
301+ var cm = new MicroCheck([])
302+ .setData(this.data);
303+
304+ // -----------------------------------------------------------
305+ {
306+ var result1 = cm.getValues("companies[1].projects[0].name");
307+ var expected1 = this.data.companies[1].projects[0].name;
308+ assertEqual(1, result1.length);
309+ assertEqual(expected1, result1[0]);
310+ }
311+
312+ // -----------------------------------------------------------
313+ {
314+ var result2 = cm.getValues("companies[1].info.members[0]");
315+ var expected2 = this.data.companies[1].info.members[0];
316+ assertEqual(expected2, result2);
317+ }
318+
319+ },
320+
321+ test_getBlobArrayValue :function(){
322+ "use strict";
323+
324+ var cm = new MicroCheck([])
325+ .setData(this.data);
326+
327+ // -----------------------------------------------------------
328+ {
329+ var result1 = cm.getValues("companies[*].projects[0].name");
330+ var expected1 = this.data.companies.map(function(c){
331+ return c.projects[0].name;
332+ });
333+ assertEqual(expected1.length, result1.length);
334+ expected1.map(function(expected, index){
335+ assertEqual(expected, result1[index]);
336+ });
337+ }
338+
339+ // -----------------------------------------------------------
340+ {
341+ var result2 = cm.getValues("companies[1].info.members[*]");
342+ var expected2 = this.data.companies[1].info.members;
343+ assertEqual(expected2.length, result2.length);
344+ expected2.map(function(expected, index){
345+ assertEqual(expected, result2[index]);
346+ });
347+ }
348+
349+ },
350+
351+ test_getBlobPropertyValue :function(){
352+ "use strict";
353+
354+ var cm = new MicroCheck([])
355+ .setData(this.data);
356+
357+ // -----------------------------------------------------------
358+ {
359+ var result1 = cm.getValues("companies.*.name");
360+ //log(result1);
361+ }
362+
363+ {
364+ var result2 = cm.getValues("companies.*.info.members");
365+ //log(result2);
366+ }
367+
368+
369+ },
370+
371+ /* */// ------------------------------------------------ //
372+
373+ test_matches :function(){
374+ "use strict";
375+
376+ var validationRules = [
377+ [
378+ "id of first company",
379+ "companies[0].id",
380+ {
381+ matches :"1",
382+ },
383+ ],
384+ [
385+ "id of 2nd company",
386+ "companies[1].id",
387+ {
388+ matches :2,
389+ },
390+ ],
391+ ];
392+
393+ var cm = new MicroCheck(validationRules)
394+ .setData(this.data);
395+
396+ var result = cm.validate();
397+
398+ // no errors
399+ assertEqual(0, result.length);
400+
401+ },
402+
403+ test_matches_fail :function(){
404+ "use strict";
405+
406+ var validationRules = [
407+ [
408+ "id of first company",
409+ "companies[0].id",
410+ {
411+ matches :"11",
412+ },
413+ ],
414+ [
415+ "id of 2nd company",
416+ "companies[1].id",
417+ {
418+ matches :22,
419+ },
420+ ],
421+ ];
422+
423+ var cm = new MicroCheck(validationRules)
424+ .setData(this.data);
425+
426+ var result = cm.validate();
427+
428+ assertEqual(2, result.length);
429+
430+ result.map(function(r){
431+ log(r);
432+ });
433+
434+ },
435+
436+ test_match_exact :function(){
437+ "use strict";
438+
439+ var validationRules = [
440+ [
441+ "first project of first company",
442+ "companies[1].projects[0].name",
443+ {
444+ matches_exact :this.data.companies[1].projects[0].name,
445+ },
446+ ],
447+ ];
448+
449+ var cm = new MicroCheck(validationRules)
450+ .setData(this.data);
451+
452+ var result = cm.validate();
453+
454+ assertEqual(0, result.length);
455+
456+ },
457+
458+ test_match_exact_fail :function(){
459+ "use strict";
460+
461+ var validationRules = [
462+ [
463+ "first project of first company",
464+ "companies[1].projects[0].name",
465+ {
466+ matches_exact :"basic design",
467+ },
468+ ],
469+ ];
470+
471+ var cm = new MicroCheck(validationRules)
472+ .setData(this.data);
473+
474+ var result = cm.validate();
475+
476+ assertEqual(1, result.length);
477+
478+ result.map(function(r){
479+ log(r);
480+ });
481+
482+ },
483+
484+ test_required :function(){
485+ "use strict";
486+
487+ var validationRules = [
488+ [
489+ "company id",
490+ "companies[*].id",
491+ {
492+ required :true,
493+ },
494+ ],
495+ ];
496+
497+ var cm = new MicroCheck(validationRules)
498+ .setData(this.data);
499+
500+ var result = cm.validate();
501+
502+ assertEqual(0, result.length);
503+
504+ },
505+
506+ test_required_null :function(){
507+ "use strict";
508+
509+ var validationRules = [
510+ [
511+ "employees type",
512+ "employees[*].type",
513+ {
514+ required :true,
515+ },
516+ ],
517+ ];
518+
519+ var cm = new MicroCheck(validationRules)
520+ .setData(this.data);
521+
522+ var result = cm.validate();
523+
524+ assertEqual(2, result.length);
525+
526+ result.map(function(r){
527+ log(r);
528+ });
529+
530+ },
531+
532+ test_ignore_required :function(){
533+ "use strict";
534+
535+ var validationRules = [
536+ [
537+ "next company",
538+ "next.*.name",
539+ {
540+ required :false,
541+ },
542+ ],
543+ ];
544+
545+ var cm = new MicroCheck(validationRules)
546+ .setData(this.data);
547+
548+ var result = cm.validate();
549+
550+ // all are not required so no validation is done
551+ assertEqual(0, result.length);
552+
553+ },
554+
555+ test_required_fail :function(){
556+ "use strict";
557+
558+ var validationRules = [
559+ [
560+ "duration in company",
561+ "companies[*].info.duration",
562+ {
563+ required :true,
564+ },
565+ ],
566+ ];
567+
568+ var cm = new MicroCheck(validationRules)
569+ .setData(this.data);
570+
571+ var result = cm.validate();
572+
573+ // there are 3 companies with info.duration
574+ assertEqual(1, result.length);
575+
576+ result.map(function(r){
577+ log(r);
578+ });
579+
580+ },
581+
582+ test_max_length :function(){
583+ "use strict";
584+
585+ var validationRules = [
586+ [
587+ "company name",
588+ "companies[*].name",
589+ {
590+ max_length :6,
591+ },
592+ ],
593+ ];
594+
595+ var cm = new MicroCheck(validationRules)
596+ .setData(this.data);
597+
598+ var result = cm.validate();
599+
600+ // all test passed
601+ assertEqual(0, result.length);
602+
603+ },
604+
605+ test_max_length_fail :function(){
606+ "use strict";
607+
608+ var validationRules = [
609+ [
610+ "company name",
611+ "companies[*].name",
612+ {
613+ max_length :2,
614+ },
615+ ],
616+ ];
617+
618+ var cm = new MicroCheck(validationRules)
619+ .setData(this.data);
620+
621+ var result = cm.validate();
622+
623+ // companies with names longer than 3
624+ assertEqual(this.data.companies.length, result.length);
625+
626+ result.map(function(r){
627+ log(r);
628+ });
629+
630+ },
631+
632+ test_min_length :function(){
633+ "use strict";
634+
635+ var validationRules = [
636+ [
637+ "company name",
638+ "companies[*].name",
639+ {
640+ min_length :2,
641+ },
642+ ],
643+ ];
644+
645+ var cm = new MicroCheck(validationRules)
646+ .setData(this.data);
647+
648+ var result = cm.validate();
649+
650+ // all test passed
651+ assertEqual(0, result.length);
652+
653+ },
654+
655+ test_min_length_fail :function(){
656+ "use strict";
657+
658+ var validationRules = [
659+ [
660+ "company name",
661+ "companies[*].name",
662+ {
663+ min_length :10,
664+ },
665+ ],
666+ ];
667+
668+ var cm = new MicroCheck(validationRules)
669+ .setData(this.data);
670+
671+ var result = cm.validate();
672+
673+ assertEqual(this.data.companies.length, result.length);
674+
675+ result.map(function(r){
676+ log(r);
677+ });
678+
679+ },
680+
681+ test_exact_length :function(){
682+ "use strict";
683+
684+ var validationRules = [
685+ [
686+ "company name",
687+ "companies[0].name",
688+ {
689+ exact_length :4,
690+ },
691+ ],
692+ ];
693+
694+ var cm = new MicroCheck(validationRules)
695+ .setData(this.data);
696+
697+ var result = cm.validate();
698+
699+ // all test passed
700+ assertEqual(0, result.length);
701+
702+ },
703+
704+ test_exact_length_fail :function(){
705+ "use strict";
706+
707+ var validationRules = [
708+ [
709+ "company name",
710+ "companies[0].name",
711+ {
712+ exact_length :10,
713+ },
714+ ],
715+ ];
716+
717+ var cm = new MicroCheck(validationRules)
718+ .setData(this.data);
719+
720+ var result = cm.validate();
721+
722+ assertEqual(1, result.length);
723+
724+ result.map(function(r){
725+ log(r);
726+ });
727+
728+ },
729+
730+ test_valid_email :function(){
731+ "use strict";
732+
733+ var validationRules = [
734+ [
735+ "ac's email",
736+ "employees[0].email",
737+ {
738+ valid_email :true,
739+ },
740+ ],
741+ ];
742+
743+ var cm = new MicroCheck(validationRules)
744+ .setData(this.data);
745+
746+ var result = cm.validate();
747+
748+ // all test passed
749+ assertEqual(0, result.length);
750+
751+ },
752+
753+ test_ignore_valid_email :function(){
754+ "use strict";
755+
756+ var validationRules = [
757+ [
758+ "aya's email",
759+ "employees[1].email",
760+ {
761+ valid_email :false,
762+ },
763+ ],
764+ ];
765+
766+ var cm = new MicroCheck(validationRules)
767+ .setData(this.data);
768+
769+ var result = cm.validate();
770+
771+ // all test passed
772+ assertEqual(0, result.length);
773+
774+ },
775+
776+ test_valid_email_fail :function(){
777+ "use strict";
778+
779+ var validationRules = [
780+ [
781+ "ac's email",
782+ "employees[*].email",
783+ {
784+ valid_email :true,
785+ },
786+ ],
787+ ];
788+
789+ var cm = new MicroCheck(validationRules)
790+ .setData(this.data);
791+
792+ var result = cm.validate();
793+
794+ assertEqual(1, result.length);
795+
796+ result.map(function(r){
797+ log(r);
798+ });
799+
800+ },
801+
802+ test_numeric :function(){
803+ "use strict";
804+
805+ var validationRules = [
806+ [
807+ "company id",
808+ "companies[*].id",
809+ {
810+ numeric :true,
811+ },
812+ ],
813+ ];
814+
815+ var cm = new MicroCheck(validationRules)
816+ .setData(this.data);
817+
818+ var result = cm.validate();
819+
820+ // all test passed
821+ assertEqual(0, result.length);
822+
823+ },
824+
825+ test_numeric_fail :function(){
826+ "use strict";
827+
828+ var validationRules = [
829+ [
830+ "company project names",
831+ "companies[*].projects[*].name",
832+ {
833+ numeric :true,
834+ },
835+ ],
836+ ];
837+
838+ var cm = new MicroCheck(validationRules)
839+ .setData(this.data);
840+
841+ var result = cm.validate();
842+
843+ var projCount =
844+ this.data.companies
845+ .reduce(function(cur, company, i){
846+ // do not include the count of projects
847+ // with no name
848+ return cur + company.projects
849+ .filter(function(p){
850+ return p.name.trim() !== "";
851+ }).length;
852+ }, 0);
853+
854+ // all test passed
855+ assertEqual(projCount, result.length);
856+
857+ result.map(function(r){
858+ log(r);
859+ });
860+
861+ },
862+
863+ test_ignore_numeric :function(){
864+ "use strict";
865+
866+ var validationRules = [
867+ [
868+ "company name",
869+ "companies[*].name",
870+ {
871+ numeric :false,
872+ },
873+ ],
874+ ];
875+
876+ var cm = new MicroCheck(validationRules)
877+ .setData(this.data);
878+
879+ var result = cm.validate();
880+
881+ // all test passed
882+ assertEqual(0, result.length);
883+
884+ },
885+
886+ test_not_numeric_fail :function(){
887+ "use strict";
888+
889+ var validationRules = [
890+ [
891+ "company id",
892+ "companies[*].id",
893+ {
894+ numeric :false,
895+ },
896+ ],
897+ ];
898+
899+ var cm = new MicroCheck(validationRules)
900+ .setData(this.data);
901+
902+ var result = cm.validate();
903+
904+ // all test passed
905+ assertEqual(this.data.companies.length, result.length);
906+
907+ result.map(function(r){
908+ log(r);
909+ });
910+
911+ },
912+
913+ test_regex_match :function(){
914+ "use strict";
915+
916+ var validationRules = [
917+ [
918+ "company code",
919+ "companies[*].code",
920+ {
921+ regex_match :"^[A-Z][0-9]{5}[A-Z](-[0-9]{3})?$",
922+ },
923+ ],
924+ ];
925+
926+ var cm = new MicroCheck(validationRules)
927+ .setData(this.data);
928+
929+ var result = cm.validate();
930+
931+ // all passed
932+ assertEqual(0, result.length);
933+ },
934+
935+ test_regex_match_fail :function(){
936+ "use strict";
937+
938+ var validationRules = [
939+ [
940+ "company code",
941+ "companies[*].code",
942+ {
943+ regex_match :"^[A-Za-z][0-9]{5}[A-Za-z]$",
944+ },
945+ ],
946+ ];
947+
948+ var cm = new MicroCheck(validationRules)
949+ .setData(this.data);
950+
951+ var result = cm.validate();
952+
953+ // only one company code does not match the expression
954+ assertEqual(1, result.length);
955+ },
956+
957+ test_alpha :function(){
958+ "use strict";
959+
960+ var validationRules = [
961+ [
962+ "company name",
963+ "companies[*].name",
964+ {
965+ alpha :true,
966+ },
967+ ],
968+ ];
969+
970+ var cm = new MicroCheck(validationRules)
971+ .setData(this.data);
972+
973+ var result = cm.validate();
974+
975+ // all passed
976+ assertEqual(0, result.length);
977+ },
978+
979+ test_ignore_alpha :function(){
980+ "use strict";
981+
982+ var validationRules = [
983+ [
984+ "company code",
985+ "companies[*].code",
986+ {
987+ // company codes are alpha numeric but will be ignored
988+ alpha :false,
989+ },
990+ ],
991+ ];
992+
993+ var cm = new MicroCheck(validationRules)
994+ .setData(this.data);
995+
996+ var result = cm.validate();
997+
998+ // all passed
999+ assertEqual(0, result.length);
1000+ },
1001+
1002+ test_alpha_fail :function(){
1003+ "use strict";
1004+
1005+ var validationRules = [
1006+ [
1007+ "company code",
1008+ "companies[*].code",
1009+ {
1010+ alpha :true,
1011+ },
1012+ ],
1013+ ];
1014+
1015+ var cm = new MicroCheck(validationRules)
1016+ .setData(this.data);
1017+
1018+ var result = cm.validate();
1019+
1020+ // company codes have number
1021+ assertEqual(this.data.companies.length, result.length);
1022+ },
1023+
1024+ test_alpha_numeric :function() {
1025+ "use strict";
1026+
1027+ var validationRules = [
1028+ [
1029+ "company code",
1030+ "companies[*].code",
1031+ {
1032+ alpha_numeric :true,
1033+ },
1034+ ],
1035+ ];
1036+
1037+ var cm = new MicroCheck(validationRules)
1038+ .setData(this.data);
1039+
1040+ var result = cm.validate();
1041+
1042+ // all passed except for axas which has a '-' in its code
1043+ assertEqual(1, result.length);
1044+ },
1045+
1046+ test_ignore_alpha_numeric :function() {
1047+ "use strict";
1048+
1049+ var validationRules = [
1050+ [
1051+ "company code",
1052+ "companies[0].code",
1053+ {
1054+ alpha_numeric :false,
1055+ },
1056+ ],
1057+ ];
1058+
1059+ var cm = new MicroCheck(validationRules)
1060+ .setData(this.data);
1061+
1062+ var result = cm.validate();
1063+
1064+ // axas which has a '-' in its code is ignored
1065+ assertEqual(0, result.length);
1066+ },
1067+
1068+ test_alpha_numeric_fail :function() {
1069+ "use strict";
1070+
1071+ var validationRules = [
1072+ [
1073+ "company code",
1074+ "companies[0].code",
1075+ {
1076+ alpha_numeric :true,
1077+ },
1078+ ],
1079+ ];
1080+
1081+ var cm = new MicroCheck(validationRules)
1082+ .setData(this.data);
1083+
1084+ var result = cm.validate();
1085+
1086+ // axas has a '-' in its code
1087+ assertEqual(1, result.length);
1088+ },
1089+
1090+ test_single_byte :function(){
1091+ "use strict";
1092+
1093+ var validationRules = [
1094+ [
1095+ "company names",
1096+ "companies[*].name",
1097+ {
1098+ single_byte :true,
1099+ },
1100+ ],
1101+ ];
1102+
1103+ var cm = new MicroCheck(validationRules)
1104+ .setData(this.data);
1105+
1106+ var result = cm.validate();
1107+
1108+ // all company names are single byte
1109+ assertEqual(0, result.length);
1110+ },
1111+
1112+ test_ignore_single_byte :function(){
1113+ "use strict";
1114+
1115+ var validationRules = [
1116+ [
1117+ "kds members",
1118+ "companies[0].info.members[*]",
1119+ {
1120+ // kds member names are double byte but will be ignored
1121+ single_byte :false,
1122+ },
1123+ ],
1124+ ];
1125+
1126+ var cm = new MicroCheck(validationRules)
1127+ .setData(this.data);
1128+
1129+ var result = cm.validate();
1130+
1131+ // all company names are single byte
1132+ assertEqual(0, result.length);
1133+ },
1134+
1135+ test_single_byte_fail :function(){
1136+ "use strict";
1137+
1138+ var validationRules = [
1139+ [
1140+ "kds members",
1141+ "companies[0].info.members[*]",
1142+ {
1143+ single_byte :true,
1144+ },
1145+ ],
1146+ ];
1147+
1148+ var cm = new MicroCheck(validationRules)
1149+ .setData(this.data);
1150+
1151+ var result = cm.validate();
1152+
1153+ // all kds members have japanese names
1154+ assertEqual(this.data.companies[0].info.members.length, result.length);
1155+ },
1156+
1157+ test_single_byte_fail2 :function(){
1158+ "use strict";
1159+
1160+ var doubleByteData = {
1161+ double3 :"0123456789",
1162+ doubleC :"ABCDEFGHIJ",
1163+ halfKana :"カタカナ",
1164+ };
1165+
1166+ var validationRules = [
1167+ [
1168+ "double byte number",
1169+ "double3",
1170+ {
1171+ single_byte :true,
1172+ },
1173+ ],
1174+ [
1175+ "double byte letter",
1176+ "doubleC",
1177+ {
1178+ single_byte :true,
1179+ },
1180+ ],
1181+ [
1182+ "half width kana",
1183+ "halfKana",
1184+ {
1185+ single_byte :true,
1186+ },
1187+ ],
1188+ ];
1189+
1190+ var cm = new MicroCheck(validationRules)
1191+ .setData(doubleByteData);
1192+
1193+ var result = cm.validate();
1194+
1195+ assertEqual(3, result.length);
1196+ },
1197+
1198+ /* */// ------------------------------------------------ //
1199+ /* */// tearDown, shutDown
1200+ /* */// ------------------------------------------------ //
1201+
1202+ tearDown :function(){
1203+
1204+ },
1205+
1206+ shutDown :function(){
1207+ "use strict";
1208+
1209+ result.log();
1210+ },
1211+
1212+};
Show on old repository browser