11'use strict' ;
2+ /*global Promise*/
23
34var util = require ( './util' ) ;
45var Ajv = require ( 'ajv' ) ;
@@ -32,13 +33,26 @@ JSONScript.prototype.addInstruction = addInstruction;
3233JSONScript . prototype . addExecutor = addExecutor ;
3334
3435
36+ /**
37+ * validates script
38+ * @this JSONScript
39+ * @param {Any } script script can be any allowed value
40+ * @return {Boolean } validation result
41+ */
3542function validateScript ( script ) {
3643 var valid = this . _validate ( script ) ;
3744 validateScript . errors = this . _validate . errors ;
3845 return valid ;
3946}
4047
4148
49+ /**
50+ * evaluates script
51+ * @this JSONScript
52+ * @param {Any } script any valid script
53+ * @param {Any } data data instance that can be referenced from the script
54+ * @return {Any } evaluation result
55+ */
4256function evaluateScript ( script , data ) {
4357 var wrapped = { script : script } ;
4458 var valid ;
@@ -54,27 +68,43 @@ function evaluateScript(script, data) {
5468}
5569
5670
57- function addInstruction ( definition , keywordFunc , regenerateSchemas ) {
71+ /**
72+ * add JSONScript instruction to the interpreter
73+ * @this JSONScript
74+ * @param {Object } definition instruction definition, should be valid according to the schema http://json-script.com/schema/instruction.json#
75+ * @param {Function } keywordFunc function to implement the instruction, accepts instruction object and dataPath as parameter, should return sync/async value or Script instance
76+ * @param {Boolean } _regenerateSchemas pass false to prevent regenerating the schemas, can be used when multiple instructions are added
77+ */
78+ function addInstruction ( definition , keywordFunc , _regenerateSchemas ) {
5879 var valid = this . _validateInstruction ( definition ) ;
5980 if ( ! valid ) throw new Ajv . ValidationError ( this . _validateInstruction . errors ) ;
6081 // TODO check instruction is unique
6182 this . _instructions . push ( definition ) ;
6283 var keyword = definition . evaluate . validatorKeyword ;
6384 this . _evalKeywords [ keyword ] = keywordFunc ;
6485 addAjvKeyword . call ( this , keyword , 'object' , true ) ;
65- if ( regenerateSchemas !== false ) generateSchemas ( ) ;
86+ if ( _regenerateSchemas !== false ) generateSchemas ( ) ;
6687}
6788
6889
90+ /**
91+ * add external executor to JSONScript interpreter
92+ * @this JSONScript
93+ * @param {String } name executor name to use in $exec keyword
94+ * @param {Object|Function } executor executor object or function
95+ */
6996function addExecutor ( name , executor ) {
7097 // TODO check duplicates, show warnings
7198 // ? TODO whitelist methods?
7299 this . _executors [ name ] = executor ;
73100}
74101
75102
103+ /**
104+ * private function to add Ajv keywords that are used in the schema that evaluates scripts
105+ * @this JSONScript
106+ */
76107function addAjvKeywords ( ) {
77- var self = this ;
78108 addAjvKeyword . call ( this , 'validateAsync' ) ;
79109 addAjvKeyword . call ( this , 'itemsSerial' , 'array' ) ;
80110 this . _evalKeywords . objectToAsync = util . objectToPromise ;
@@ -86,6 +116,13 @@ function addAjvKeywords() {
86116}
87117
88118
119+ /**
120+ * private function to add Ajv keyword that is used for instruction/script evaluation
121+ * @this JSONScript
122+ * @param {String } keyword custom validation keyword
123+ * @param {String|Array<String> } types type(s) that the keyword applies for
124+ * @param {Boolean } inlineInstruction true to use evaluationKeywords.inlineInstruction as keyword function, otherwise evaluationKeywords[keyword] is used
125+ */
89126function addAjvKeyword ( keyword , types , inlineInstruction ) {
90127 var inlineFunc = evaluationKeywords [ inlineInstruction ? 'inlineInstruction' : keyword ] ;
91128 this . ajv . addKeyword ( keyword , {
@@ -97,6 +134,10 @@ function addAjvKeyword(keyword, types, inlineInstruction) {
97134}
98135
99136
137+ /**
138+ * private function to add all standard JSONScript instructions to the interpreter
139+ * @this JSONScript
140+ */
100141function addCoreInstructions ( ) {
101142 this . _validateInstruction = this . ajv . compile ( require ( 'jsonscript/schema/instruction.json' ) ) ;
102143 instructions . forEach ( function ( inst ) {
@@ -106,6 +147,10 @@ function addCoreInstructions() {
106147}
107148
108149
150+ /**
151+ * private function to regenerate validation and evaluation schemas, called when an instruction is added
152+ * @this JSONScript
153+ */
109154function generateSchemas ( ) {
110155 this . ajv . addMetaSchema ( _generate . call ( this , 'evaluate_metaschema' ) ) ;
111156 this . _validate = this . ajv . compile ( _generate . call ( this , 'schema' ) ) ;
@@ -114,13 +159,23 @@ function generateSchemas() {
114159}
115160
116161
162+ /**
163+ * private function to generate one of schemas used by the interpreter
164+ * @this JSONScript
165+ * @param {String } schemaName schema name
166+ * @return {Object } generated schema object
167+ */
117168function _generate ( schemaName ) {
118169 var schema = generateSchema ( schemaName , this . _instructions ) ;
119170 this . ajv . removeSchema ( schema . id ) ;
120171 return schema ;
121172}
122173
123174
175+ /**
176+ * Constructor for Script object. Instructions can return the instance (as "new this.js.Script(script)") if the returned value is a script that should be evaluated
177+ * @param {Any } script any valid script
178+ */
124179function Script ( script ) {
125180 this . script = script ;
126181}
0 commit comments