hints.js 2.9 KB

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