refactor: convert custom.def partial into custom.jst function (for better encapsulation)

master
Evgeny Poberezkin 2016-07-18 22:14:02 +01:00
parent 1a6e4b576c
commit caf7557bc0
No known key found for this signature in database
GPG Key ID: 016D62451CED9D8E
3 changed files with 14 additions and 11 deletions

View File

@ -1,10 +1,11 @@
{{# def.definitions }}
{{# def.errors }}
{{# def.setupKeyword }}
{{
var $schema = it.schema[$rule.keyword]
var $rule = this
, $ruleValidate = it.useCustomRule($rule, $schema, it.schema, it)
, $ruleErrs = $ruleValidate.code + '.errors'
, $schemaPath = it.schemaPath + '.' + $rule.keyword
, $errSchemaPath = it.errSchemaPath + '/' + $rule.keyword
, $errs = 'errs' + $lvl
, $i = 'i' + $lvl
, $ruleErr = 'ruleErr' + $lvl
, $rDef = $rule.definition
@ -16,6 +17,7 @@
throw new Error('async keyword in sync schema');
}}
{{? !($inline || $macro) }}{{=$ruleErrs}} = null;{{?}}
var {{=$errs}} = errors;
var valid{{=$lvl}};
@ -146,6 +148,5 @@ if (!{{# def.ruleValidationResult }}) {
}
{{?}}
{{?}}
{{ $errorKeyword = undefined; }}
} {{? $breakOnError }} else { {{?}}

View File

@ -106,11 +106,7 @@
{{?}}
{{~ $rulesGroup.rules:$rule }}
{{? $shouldUseRule($rule) }}
{{? $rule.custom }}
{{# def.custom }}
{{??}}
{{= $rule.code(it, $rule.keyword) }}
{{?}}
{{= $rule.code(it, $rule.keyword) }}
{{? $breakOnError }}
{{ $closingBraces1 += '}'; }}
{{?}}

View File

@ -1,6 +1,7 @@
'use strict';
var IDENTIFIER = /^[a-z_$][a-z0-9_$]*$/i;
var customRuleCode = require('./dotjs/custom');
/**
* Define custom keyword
@ -51,7 +52,12 @@ module.exports = function addKeyword(keyword, definition) {
self.RULES.push(ruleGroup);
}
var rule = { keyword: keyword, definition: definition, custom: true };
var rule = {
keyword: keyword,
definition: definition,
custom: true,
code: customRuleCode
};
ruleGroup.rules.push(rule);
}