hints.js 2.7 KB

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