@lowdefy/ajv 4.0.0-alpha.29 → 4.0.0-alpha.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,30 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
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
+ */ import { nunjucksFunction } from '@lowdefy/nunjucks';
16
+ function createErrorMessage(errors = []) {
17
+ if (errors.length === 1) {
18
+ const template = nunjucksFunction(errors[0].message);
19
+ return template(errors[0]);
20
+ }
21
+ if (errors.length > 1) {
22
+ const firstMessage = errors[0].message;
23
+ const lastMessage = errors[errors.length - 1].message;
24
+ const firstTemplate = nunjucksFunction(firstMessage);
25
+ const lastTemplate = nunjucksFunction(lastMessage);
26
+ return `${firstTemplate(errors[0])}; ${lastTemplate(errors[errors.length - 1])}`;
27
+ }
28
+ return 'Schema validation error.';
29
+ }
30
+ export default createErrorMessage;
package/dist/index.js ADDED
@@ -0,0 +1,16 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
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
+ */ import validate from './validate.js';
16
+ export { validate };
@@ -0,0 +1,38 @@
1
+ /*
2
+ Copyright 2020-2022 Lowdefy, Inc
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
+ */ import Ajv from 'ajv';
16
+ import ajvErrors from 'ajv-errors';
17
+ import createErrorMessage from './createErrorMessage.js';
18
+ const ajv = new Ajv({
19
+ allErrors: true,
20
+ strict: false
21
+ });
22
+ ajvErrors(ajv);
23
+ function validate({ schema , data , returnErrors =false }) {
24
+ const valid = ajv.validate(schema, data);
25
+ if (!valid) {
26
+ if (returnErrors) {
27
+ return {
28
+ valid: false,
29
+ errors: ajv.errors
30
+ };
31
+ }
32
+ throw new Error(createErrorMessage(ajv.errors));
33
+ }
34
+ return {
35
+ valid: true
36
+ };
37
+ }
38
+ export default validate;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lowdefy/ajv",
3
- "version": "4.0.0-alpha.29",
3
+ "version": "4.0.0-alpha.31",
4
4
  "license": "Apache-2.0",
5
5
  "description": "",
6
6
  "homepage": "https://lowdefy.com",
@@ -39,7 +39,7 @@
39
39
  "test": "jest --coverage"
40
40
  },
41
41
  "dependencies": {
42
- "@lowdefy/nunjucks": "4.0.0-alpha.29",
42
+ "@lowdefy/nunjucks": "4.0.0-alpha.31",
43
43
  "ajv": "8.11.0",
44
44
  "ajv-errors": "3.0.0"
45
45
  },
@@ -52,5 +52,5 @@
52
52
  "publishConfig": {
53
53
  "access": "public"
54
54
  },
55
- "gitHead": "621a191ebc0a1569ee6669dc74c12f8be5a8c7f3"
55
+ "gitHead": "96ef86d4ce4849f8f11110662efbbaede1bcd5a5"
56
56
  }