1+ /*
2+ * Copyright (c) 2020 MarkLogic Corporation
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+ 'use strict' ;
17+
18+ const Ajv = require ( 'ajv' ) ;
19+
20+ const endpointDeclarationSchema =
21+ { "$schema" : "http://json-schema.org/draft-07/schema#" ,
22+ "title" : "MarkLogic Endpoint Function Declaration" ,
23+ "$comment" : "SIMPLIFIED TO THE STABLE DECLARATIONS USED FOR CODE GENERATION" ,
24+ "type" : "object" ,
25+ "definitions" : {
26+ "desc" : {
27+ "type" :"string" , "description" :"Documentation about the property"
28+ } ,
29+ "datatype" : {
30+ "type" :"string" , "description" :"The type of the value" ,
31+ "enum" :[
32+ "boolean" , "date" , "dateTime" , "dayTimeDuration" , "decimal" , "double" , "float" ,
33+ "int" , "long" , "string" , "time" , "unsignedInt" , "unsignedLong" ,
34+ "array" , "object" ,
35+ "binaryDocument" , "jsonDocument" , "textDocument" , "xmlDocument" ,
36+ "session"
37+ ]
38+ } ,
39+ "nullable" : {
40+ "type" :"boolean" , "description" :"Whether a null value is allowed" ,
41+ "default" :false
42+ } ,
43+ "multiple" : {
44+ "type" :"boolean" , "description" :"Whether multiple values are allowed" ,
45+ "default" :false
46+ }
47+ // "$comment": "SIMPLIFIED BY DELETING doubleMeter, doubleLiteral, ulMeter, AND unsignedLongLiteral"
48+ } ,
49+ "propertyNames" : {
50+ // "$comment": "MODIFIED TO ALLOW FOR PROPERTIES DELETED DURING SIMPLIFICATION OR ADDED IN LATER RELEASES",
51+ "pattern" : "^\\$?[A-Za-z_][\\w.-]*$"
52+ } ,
53+ "properties" : {
54+ "functionName" : {
55+ "type" :"string" , "description" :"The name of a database function provided by a service declared by service.json"
56+ } ,
57+ "endpoint" : {
58+ "type" :"string" , "description" :"The full path name of a standalone bulk IO endpoint"
59+ } ,
60+ "desc" : { "$ref" :"#/definitions/desc" } ,
61+ "params" : {
62+ "type" :"array" , "description" :"The parameters of the function" ,
63+ "items" : {
64+ "type" :"object" ,
65+ "required" : [ "name" , "datatype" ] ,
66+ "propertyNames" : {
67+ "pattern" : "^(\\$[A-Za-z_][\\w.-]*|name|desc|datatype|nullable|multiple)$"
68+ } ,
69+ "properties" : {
70+ "name" : {
71+ "type" :"string" , "description" :"The name of the function parameter"
72+ } ,
73+ "desc" : { "$ref" :"#/definitions/desc" } ,
74+ "datatype" : { "$ref" :"#/definitions/datatype" } ,
75+ "nullable" : { "$ref" :"#/definitions/nullable" } ,
76+ "multiple" : { "$ref" :"#/definitions/multiple" }
77+ }
78+ }
79+ } ,
80+ "return" : {
81+ "type" :"object" , "description" :"The return value of the function" ,
82+ "required" : [ "datatype" ] ,
83+ "propertyNames" : {
84+ "pattern" : "^(\\$[A-Za-z_][\\w.-]*|desc|datatype|nullable|multiple)$"
85+ } ,
86+ "properties" : {
87+ "desc" : { "$ref" :"#/definitions/desc" } ,
88+ "datatype" : { "$ref" :"#/definitions/datatype" } ,
89+ "nullable" : { "$ref" :"#/definitions/nullable" } ,
90+ "multiple" : { "$ref" :"#/definitions/multiple" }
91+ }
92+ }
93+ // "$comment": "SIMPLIFIED BY DELETING errorDetail AND monitoring"
94+ }
95+ } ;
96+
97+ const ajv = new Ajv ( {
98+ allErrors : true ,
99+ validateSchema : false // true
100+ } ) ;
101+
102+ const validateWithSchema = ajv . compile ( endpointDeclarationSchema ) ;
103+
104+ function validate ( endpointDeclaration ) {
105+ const isValid = validateWithSchema ( endpointDeclaration ) ;
106+ const result = { isValid : isValid } ;
107+ if ( ! isValid ) {
108+ result . errors = validateWithSchema . errors ;
109+ }
110+ return result ;
111+ }
112+
113+ module . exports = {
114+ validate : validate
115+ } ;
0 commit comments