all files / src/ Validator.js

84.62% Statements 11/13
66.67% Branches 4/6
100% Functions 1/1
84.62% Lines 11/13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53                                                               87× 87×     87×     87× 87×   87× 86×            
var log = require('./Log.js').Logger('libZotero:Validator');
 
var validator = {
	patterns: {
		//'itemKey': /^([A-Z0-9]{8,},?)+$/,
		'itemKey': /^.+$/,
		'collectionKey': /^([A-Z0-9]{8,})|trash$/,
		//'tag': /^[^#]*$/,
		'libraryID': /^[0-9]+$/,
		'libraryType': /^(user|group|)$/,
		'target': /^(items?|collections?|tags|children|deleted|userGroups|key|settings|publications)$/,
		'targetModifier': /^(top|file|file\/view)$/,
		
		//get params
		'sort': /^(asc|desc)$/,
		'start': /^[0-9]*$/,
		'limit': /^[0-9]*$/,
		'order': /^\S*$/,
		'content': /^((html|json|data|bib|none|bibtex|bookmarks|coins|csljson|mods|refer|rdf_bibliontology|rdf_dc|rdf_zotero|ris|tei|wikipedia),?)+$/,
		'include': /^((html|json|data|bib|none|bibtex|bookmarks|coins|csljson|mods|refer|rdf_bibliontology|rdf_dc|rdf_zotero|ris|tei|wikipedia),?)+$/,
		'format': /^((atom|bib|json|keys|versions|bibtex|bookmarks|coins|csljson|mods|refer|rdf_bibliontology|rdf_dc|rdf_zotero|ris|tei|wikipedia),?)+$/,
		'q': /^.*$/,
		'fq': /^\S*$/,
		'itemType': /^\S*$/,
		'locale': /^\S*$/,
		'tag': /^.*$/,
		'tagType': /^(0|1)$/,
		'key': /^\S*/,
		'style': /^\S*$/,
		'linkwrap': /^(0|1)*$/
	},
	
	validate: function(arg, type){
		log.debug('Zotero.validate', 4);
		Iif(arg === ''){
			return null;
		}
		else Iif(arg === null){
			return true;
		}
		log.debug(arg + ' ' + type, 4);
		var patterns = this.patterns;
		
		if(patterns.hasOwnProperty(type)){
			return patterns[type].test(arg);
		}
		else{
			return null;
		}
	}
};
 
module.exports = validator;