hints.js 2.7 KB

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