source: trip-planner-front/node_modules/schema-utils/dist/util/hints.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.9 KB
Line 
1"use strict";
2
3const Range = require('./Range');
4/** @typedef {import("../validate").Schema} Schema */
5
6/**
7 * @param {Schema} schema
8 * @param {boolean} logic
9 * @return {string[]}
10 */
11
12
13module.exports.stringHints = function stringHints(schema, logic) {
14 const hints = [];
15 let type = 'string';
16 const currentSchema = { ...schema
17 };
18
19 if (!logic) {
20 const tmpLength = currentSchema.minLength;
21 const tmpFormat = currentSchema.formatMinimum;
22 const tmpExclusive = currentSchema.formatExclusiveMaximum;
23 currentSchema.minLength = currentSchema.maxLength;
24 currentSchema.maxLength = tmpLength;
25 currentSchema.formatMinimum = currentSchema.formatMaximum;
26 currentSchema.formatMaximum = tmpFormat;
27 currentSchema.formatExclusiveMaximum = !currentSchema.formatExclusiveMinimum;
28 currentSchema.formatExclusiveMinimum = !tmpExclusive;
29 }
30
31 if (typeof currentSchema.minLength === 'number') {
32 if (currentSchema.minLength === 1) {
33 type = 'non-empty string';
34 } else {
35 const length = Math.max(currentSchema.minLength - 1, 0);
36 hints.push(`should be longer than ${length} character${length > 1 ? 's' : ''}`);
37 }
38 }
39
40 if (typeof currentSchema.maxLength === 'number') {
41 if (currentSchema.maxLength === 0) {
42 type = 'empty string';
43 } else {
44 const length = currentSchema.maxLength + 1;
45 hints.push(`should be shorter than ${length} character${length > 1 ? 's' : ''}`);
46 }
47 }
48
49 if (currentSchema.pattern) {
50 hints.push(`should${logic ? '' : ' not'} match pattern ${JSON.stringify(currentSchema.pattern)}`);
51 }
52
53 if (currentSchema.format) {
54 hints.push(`should${logic ? '' : ' not'} match format ${JSON.stringify(currentSchema.format)}`);
55 }
56
57 if (currentSchema.formatMinimum) {
58 hints.push(`should be ${currentSchema.formatExclusiveMinimum ? '>' : '>='} ${JSON.stringify(currentSchema.formatMinimum)}`);
59 }
60
61 if (currentSchema.formatMaximum) {
62 hints.push(`should be ${currentSchema.formatExclusiveMaximum ? '<' : '<='} ${JSON.stringify(currentSchema.formatMaximum)}`);
63 }
64
65 return [type].concat(hints);
66};
67/**
68 * @param {Schema} schema
69 * @param {boolean} logic
70 * @return {string[]}
71 */
72
73
74module.exports.numberHints = function numberHints(schema, logic) {
75 const hints = [schema.type === 'integer' ? 'integer' : 'number'];
76 const range = new Range();
77
78 if (typeof schema.minimum === 'number') {
79 range.left(schema.minimum);
80 }
81
82 if (typeof schema.exclusiveMinimum === 'number') {
83 range.left(schema.exclusiveMinimum, true);
84 }
85
86 if (typeof schema.maximum === 'number') {
87 range.right(schema.maximum);
88 }
89
90 if (typeof schema.exclusiveMaximum === 'number') {
91 range.right(schema.exclusiveMaximum, true);
92 }
93
94 const rangeFormat = range.format(logic);
95
96 if (rangeFormat) {
97 hints.push(rangeFormat);
98 }
99
100 if (typeof schema.multipleOf === 'number') {
101 hints.push(`should${logic ? '' : ' not'} be multiple of ${schema.multipleOf}`);
102 }
103
104 return hints;
105};
Note: See TracBrowser for help on using the repository browser.