Project

General

Profile

Download (4.07 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / common / jsonschema.js @ 9bee4afa

1
/* SPDX-License-Identifier: MIT AND CC0-1.0
2
 *
3
 * License text of the original lib/index.js from jsonschema library:
4
 *
5
 ***************************************
6
 *
7
 * jsonschema is licensed under MIT license.
8
 *
9
 * Copyright (C) 2012-2015 Tom de Grunt <tom@degrunt.nl>
10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
12
 * this software and associated documentation files (the "Software"), to deal in
13
 * the Software without restriction, including without limitation the rights to
14
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
15
 * of the Software, and to permit persons to whom the Software is furnished to do
16
 * so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in all
19
 * copies or substantial portions of the Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27
 * SOFTWARE.
28
 *
29
 *******************************************************************************
30
 *
31
 * License notice for the adaptation to use in Haketilo:
32
 *
33
 ***************************************
34
 *
35
 * Copyright (C) 2022 Wojtek Kosior <koszko@koszko.org>
36
 *
37
 * This program is free software: you can redistribute it and/or modify
38
 * it under the terms of the CC0 1.0 Universal License as published by
39
 * the Creative Commons Corporation.
40
 *
41
 * This program is distributed in the hope that it will be useful,
42
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
43
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44
 * CC0 1.0 Universal License for more details.
45
 */
46

    
47
#FROM common/jsonschema/validator.js IMPORT Validator
48
#EXPORT Validator
49

    
50
#FROM common/jsonschema/helpers.js IMPORT ValidatorResult, ValidationError, \
51
                                          ValidatorResultError, SchemaError
52

    
53
#EXPORT ValidatorResult
54
#EXPORT ValidationError
55
#EXPORT ValidatorResultError
56
#EXPORT SchemaError
57

    
58
#FROM common/jsonschema/scan.js IMPORT SchemaScanResult, scan
59

    
60
#EXPORT scan
61
#EXPORT SchemaScanResult
62

    
63
function validate(instance, schema, options) {
64
    var v = new Validator();
65
    return v.validate(instance, schema, options);
66
};
67
#EXPORT validate
68

    
69
const haketilo_schemas = [
70
    /* 1.x Hydrilla JSON schema series */
71
#INCLUDE schemas/1.x/api_query_result-1.0.1.schema.json
72
    ,
73
#INCLUDE schemas/1.x/api_mapping_description-1.0.1.schema.json
74
    ,
75
#INCLUDE schemas/1.x/api_resource_description-1.0.1.schema.json
76
    ,
77
#INCLUDE schemas/1.x/common_definitions-1.0.1.schema.json
78
    ,
79
    /* 2.x Hydrilla JSON schema series */
80
#INCLUDE schemas/2.x/api_query_result-2.schema.json
81
    ,
82
#INCLUDE schemas/2.x/api_mapping_description-2.schema.json
83
    ,
84
#INCLUDE schemas/2.x/api_resource_description-2.schema.json
85
    ,
86
#INCLUDE schemas/2.x/common_definitions-2.schema.json
87
].reduce((ac, s) => Object.assign(ac, {[s.$id]: s}), {});
88

    
89
const name_base_re    = "(?<name_base>[^/]*)";
90
const major_number_re = "(?<major>[1-9][0-9]*)";
91
const minor_number_re = "(?:[1-9][0-9]*|0)";
92
const numbers_rest_re = `(?:\\.${minor_number_re})*`;
93
const version_re      = `(?<ver>${major_number_re}${numbers_rest_re})`;
94
const schema_name_re  = `${name_base_re}-${version_re}\\.schema\\.json`;
95

    
96
const haketilo_schema_name_regex = new RegExp(schema_name_re);
97

    
98
for (const [$id, schema] of [...Object.entries(haketilo_schemas)]) {
99
    const match = haketilo_schema_name_regex.exec($id);
100
    const schema_name =
101
	  `${match.groups.name_base}-${match.groups.major}.schema.json`;
102
    haketilo_schemas[schema_name] = schema;
103
}
104

    
105
#EXPORT haketilo_schemas
106
#EXPORT haketilo_schema_name_regex
107

    
108
const haketilo_validator = new Validator();
109
Object.values(haketilo_schemas)
110
    .forEach(s => haketilo_validator.addSchema(s, s.$id));
111
#EXPORT haketilo_validator
(5-5/11)