params-schema.spec.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Copyright (c) 2023 mol* contributors, licensed under MIT, See LICENSE file for more info.
  3. *
  4. * @author Adam Midlik <midlik@gmail.com>
  5. */
  6. import * as iots from 'io-ts';
  7. import { fieldValidationIssues, RequiredField, literal, nullable, paramsValidationIssues, OptionalField } from '../params-schema';
  8. describe('fieldValidationIssues', () => {
  9. it('fieldValidationIssues string', async () => {
  10. const stringField = RequiredField(iots.string);
  11. expect(fieldValidationIssues(stringField, 'hello')).toBeUndefined();
  12. expect(fieldValidationIssues(stringField, '')).toBeUndefined();
  13. expect(fieldValidationIssues(stringField, 5)).toBeTruthy();
  14. expect(fieldValidationIssues(stringField, null)).toBeTruthy();
  15. expect(fieldValidationIssues(stringField, undefined)).toBeTruthy();
  16. });
  17. it('fieldValidationIssues string choice', async () => {
  18. const colorParam = RequiredField(literal('red', 'green', 'blue', 'yellow'));
  19. expect(fieldValidationIssues(colorParam, 'red')).toBeUndefined();
  20. expect(fieldValidationIssues(colorParam, 'green')).toBeUndefined();
  21. expect(fieldValidationIssues(colorParam, 'blue')).toBeUndefined();
  22. expect(fieldValidationIssues(colorParam, 'yellow')).toBeUndefined();
  23. expect(fieldValidationIssues(colorParam, 'banana')).toBeTruthy();
  24. expect(fieldValidationIssues(colorParam, 5)).toBeTruthy();
  25. expect(fieldValidationIssues(colorParam, null)).toBeTruthy();
  26. expect(fieldValidationIssues(colorParam, undefined)).toBeTruthy();
  27. });
  28. it('fieldValidationIssues number choice', async () => {
  29. const numberParam = RequiredField(literal(1, 2, 3, 4));
  30. expect(fieldValidationIssues(numberParam, 1)).toBeUndefined();
  31. expect(fieldValidationIssues(numberParam, 2)).toBeUndefined();
  32. expect(fieldValidationIssues(numberParam, 3)).toBeUndefined();
  33. expect(fieldValidationIssues(numberParam, 4)).toBeUndefined();
  34. expect(fieldValidationIssues(numberParam, 5)).toBeTruthy();
  35. expect(fieldValidationIssues(numberParam, '1')).toBeTruthy();
  36. expect(fieldValidationIssues(numberParam, null)).toBeTruthy();
  37. expect(fieldValidationIssues(numberParam, undefined)).toBeTruthy();
  38. });
  39. it('fieldValidationIssues int', async () => {
  40. const numberParam = RequiredField(iots.Integer);
  41. expect(fieldValidationIssues(numberParam, 1)).toBeUndefined();
  42. expect(fieldValidationIssues(numberParam, 0)).toBeUndefined();
  43. expect(fieldValidationIssues(numberParam, 0.5)).toBeTruthy();
  44. expect(fieldValidationIssues(numberParam, '1')).toBeTruthy();
  45. expect(fieldValidationIssues(numberParam, null)).toBeTruthy();
  46. expect(fieldValidationIssues(numberParam, undefined)).toBeTruthy();
  47. });
  48. it('fieldValidationIssues union', async () => {
  49. const stringOrNumberParam = RequiredField(iots.union([iots.string, iots.number]));
  50. expect(fieldValidationIssues(stringOrNumberParam, 1)).toBeUndefined();
  51. expect(fieldValidationIssues(stringOrNumberParam, 2)).toBeUndefined();
  52. expect(fieldValidationIssues(stringOrNumberParam, 'hello')).toBeUndefined();
  53. expect(fieldValidationIssues(stringOrNumberParam, '')).toBeUndefined();
  54. expect(fieldValidationIssues(stringOrNumberParam, true)).toBeTruthy();
  55. expect(fieldValidationIssues(stringOrNumberParam, null)).toBeTruthy();
  56. expect(fieldValidationIssues(stringOrNumberParam, undefined)).toBeTruthy();
  57. });
  58. it('fieldValidationIssues nullable', async () => {
  59. const stringOrNullParam = RequiredField(nullable(iots.string));
  60. expect(fieldValidationIssues(stringOrNullParam, 'hello')).toBeUndefined();
  61. expect(fieldValidationIssues(stringOrNullParam, '')).toBeUndefined();
  62. expect(fieldValidationIssues(stringOrNullParam, null)).toBeUndefined();
  63. expect(fieldValidationIssues(stringOrNullParam, 1)).toBeTruthy();
  64. expect(fieldValidationIssues(stringOrNullParam, true)).toBeTruthy();
  65. expect(fieldValidationIssues(stringOrNullParam, undefined)).toBeTruthy();
  66. });
  67. });
  68. const schema = {
  69. name: OptionalField(iots.string),
  70. surname: RequiredField(iots.string),
  71. lunch: RequiredField(iots.boolean),
  72. age: OptionalField(iots.number),
  73. };
  74. describe('validateParams', () => {
  75. it('validateParams', async () => {
  76. expect(paramsValidationIssues(schema, { surname: 'Doe', lunch: true }, { noExtra: true })).toBeUndefined();
  77. expect(paramsValidationIssues(schema, { name: 'John', surname: 'Doe', lunch: true }, { noExtra: true })).toBeUndefined();
  78. expect(paramsValidationIssues(schema, { surname: 'Doe', lunch: true, age: 29 }, { noExtra: true })).toBeUndefined();
  79. expect(paramsValidationIssues(schema, { name: 'John', surname: 'Doe', lunch: true, age: 29 }, { noExtra: true })).toBeUndefined();
  80. expect(paramsValidationIssues(schema, {}, { noExtra: true })).toBeTruthy();
  81. expect(paramsValidationIssues(schema, { name: 'John', surname: 'Doe', age: 29 }, { noExtra: true })).toBeTruthy(); // missing `lunch`
  82. expect(paramsValidationIssues(schema, { name: 'John', surname: 'Doe', lunch: true, age: 'old' }, { noExtra: true })).toBeTruthy(); // wrong type of `age`
  83. expect(paramsValidationIssues(schema, { surname: 'Doe', lunch: true, married: false }, { noExtra: true })).toBeTruthy(); // extra param `married`
  84. expect(paramsValidationIssues(schema, { surname: 'Doe', lunch: true, married: false })).toBeUndefined(); // extra param `married`
  85. });
  86. });
  87. describe('validateFullParams', () => {
  88. it('validateFullParams', async () => {
  89. expect(paramsValidationIssues(schema, { surname: 'Doe', lunch: true }, { requireAll: true, noExtra: true })).toBeTruthy();
  90. expect(paramsValidationIssues(schema, { name: 'John', surname: 'Doe', lunch: true }, { requireAll: true, noExtra: true })).toBeTruthy();
  91. expect(paramsValidationIssues(schema, { surname: 'Doe', lunch: true, age: 29 }, { requireAll: true, noExtra: true })).toBeTruthy();
  92. expect(paramsValidationIssues(schema, { name: 'John', surname: 'Doe', lunch: true, age: 29 }, { requireAll: true, noExtra: true })).toBeUndefined();
  93. expect(paramsValidationIssues(schema, {}, { requireAll: true, noExtra: true })).toBeTruthy();
  94. expect(paramsValidationIssues(schema, { name: 'John', surname: 'Doe', lunch: true, age: 'old' }, { requireAll: true, noExtra: true })).toBeTruthy(); // wrong type of `age`
  95. expect(paramsValidationIssues(schema, { name: 'John', surname: 'Doe', lunch: true, age: 29, married: true }, { requireAll: true, noExtra: true })).toBeTruthy(); // extra param `married`
  96. expect(paramsValidationIssues(schema, { name: 'John', surname: 'Doe', lunch: true, age: 29, married: true }, { requireAll: true, noExtra: false })).toBeUndefined(); // extra param `married`
  97. });
  98. });