[79a0317] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | const Range = require("./Range");
|
---|
| 4 |
|
---|
| 5 | /** @typedef {import("../validate").Schema} Schema */
|
---|
| 6 |
|
---|
| 7 | /**
|
---|
| 8 | * @param {Schema} schema
|
---|
| 9 | * @param {boolean} logic
|
---|
| 10 | * @return {string[]}
|
---|
| 11 | */
|
---|
| 12 | module.exports.stringHints = function stringHints(schema, logic) {
|
---|
| 13 | const hints = [];
|
---|
| 14 | let type = "string";
|
---|
| 15 | const currentSchema = {
|
---|
| 16 | ...schema
|
---|
| 17 | };
|
---|
| 18 | if (!logic) {
|
---|
| 19 | const tmpLength = currentSchema.minLength;
|
---|
| 20 | const tmpFormat = currentSchema.formatMinimum;
|
---|
| 21 | currentSchema.minLength = currentSchema.maxLength;
|
---|
| 22 | currentSchema.maxLength = tmpLength;
|
---|
| 23 | currentSchema.formatMinimum = currentSchema.formatMaximum;
|
---|
| 24 | currentSchema.formatMaximum = tmpFormat;
|
---|
| 25 | }
|
---|
| 26 | if (typeof currentSchema.minLength === "number") {
|
---|
| 27 | if (currentSchema.minLength === 1) {
|
---|
| 28 | type = "non-empty string";
|
---|
| 29 | } else {
|
---|
| 30 | const length = Math.max(currentSchema.minLength - 1, 0);
|
---|
| 31 | hints.push(`should be longer than ${length} character${length > 1 ? "s" : ""}`);
|
---|
| 32 | }
|
---|
| 33 | }
|
---|
| 34 | if (typeof currentSchema.maxLength === "number") {
|
---|
| 35 | if (currentSchema.maxLength === 0) {
|
---|
| 36 | type = "empty string";
|
---|
| 37 | } else {
|
---|
| 38 | const length = currentSchema.maxLength + 1;
|
---|
| 39 | hints.push(`should be shorter than ${length} character${length > 1 ? "s" : ""}`);
|
---|
| 40 | }
|
---|
| 41 | }
|
---|
| 42 | if (currentSchema.pattern) {
|
---|
| 43 | hints.push(`should${logic ? "" : " not"} match pattern ${JSON.stringify(currentSchema.pattern)}`);
|
---|
| 44 | }
|
---|
| 45 | if (currentSchema.format) {
|
---|
| 46 | hints.push(`should${logic ? "" : " not"} match format ${JSON.stringify(currentSchema.format)}`);
|
---|
| 47 | }
|
---|
| 48 | if (currentSchema.formatMinimum) {
|
---|
| 49 | hints.push(`should be ${currentSchema.formatExclusiveMinimum ? ">" : ">="} ${JSON.stringify(currentSchema.formatMinimum)}`);
|
---|
| 50 | }
|
---|
| 51 | if (currentSchema.formatMaximum) {
|
---|
| 52 | hints.push(`should be ${currentSchema.formatExclusiveMaximum ? "<" : "<="} ${JSON.stringify(currentSchema.formatMaximum)}`);
|
---|
| 53 | }
|
---|
| 54 | return [type].concat(hints);
|
---|
| 55 | };
|
---|
| 56 |
|
---|
| 57 | /**
|
---|
| 58 | * @param {Schema} schema
|
---|
| 59 | * @param {boolean} logic
|
---|
| 60 | * @return {string[]}
|
---|
| 61 | */
|
---|
| 62 | module.exports.numberHints = function numberHints(schema, logic) {
|
---|
| 63 | const hints = [schema.type === "integer" ? "integer" : "number"];
|
---|
| 64 | const range = new Range();
|
---|
| 65 | if (typeof schema.minimum === "number") {
|
---|
| 66 | range.left(schema.minimum);
|
---|
| 67 | }
|
---|
| 68 | if (typeof schema.exclusiveMinimum === "number") {
|
---|
| 69 | range.left(schema.exclusiveMinimum, true);
|
---|
| 70 | }
|
---|
| 71 | if (typeof schema.maximum === "number") {
|
---|
| 72 | range.right(schema.maximum);
|
---|
| 73 | }
|
---|
| 74 | if (typeof schema.exclusiveMaximum === "number") {
|
---|
| 75 | range.right(schema.exclusiveMaximum, true);
|
---|
| 76 | }
|
---|
| 77 | const rangeFormat = range.format(logic);
|
---|
| 78 | if (rangeFormat) {
|
---|
| 79 | hints.push(rangeFormat);
|
---|
| 80 | }
|
---|
| 81 | if (typeof schema.multipleOf === "number") {
|
---|
| 82 | hints.push(`should${logic ? "" : " not"} be multiple of ${schema.multipleOf}`);
|
---|
| 83 | }
|
---|
| 84 | return hints;
|
---|
| 85 | }; |
---|