validate-custom-fields-config.spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { Type } from '@vendure/common/lib/shared-types';
  2. import { describe, expect, it } from 'vitest';
  3. import { CustomFields } from '../config/custom-field/custom-field-types';
  4. import { coreEntitiesMap } from './entities';
  5. import { validateCustomFieldsConfig } from './validate-custom-fields-config';
  6. describe('validateCustomFieldsConfig()', () => {
  7. const allEntities = Object.values(coreEntitiesMap) as Array<Type<any>>;
  8. it('valid config', () => {
  9. const config: CustomFields = {
  10. Product: [
  11. { name: 'foo', type: 'string' },
  12. { name: 'bar', type: 'localeString' },
  13. ],
  14. };
  15. const result = validateCustomFieldsConfig(config, allEntities);
  16. expect(result.valid).toBe(true);
  17. expect(result.errors.length).toBe(0);
  18. });
  19. it('invalid localeString', () => {
  20. const config: CustomFields = {
  21. User: [
  22. { name: 'foo', type: 'string' },
  23. { name: 'bar', type: 'localeString' },
  24. ],
  25. };
  26. const result = validateCustomFieldsConfig(config, allEntities);
  27. expect(result.valid).toBe(false);
  28. expect(result.errors).toEqual(['User entity does not support custom fields of type "localeString"']);
  29. });
  30. it('valid names', () => {
  31. const config: CustomFields = {
  32. User: [
  33. { name: 'love2code', type: 'string' },
  34. { name: 'snake_case', type: 'string' },
  35. { name: 'camelCase', type: 'string' },
  36. { name: 'SHOUTY', type: 'string' },
  37. ],
  38. };
  39. const result = validateCustomFieldsConfig(config, allEntities);
  40. expect(result.valid).toBe(true);
  41. expect(result.errors).toEqual([]);
  42. });
  43. it('invalid names', () => {
  44. const config: CustomFields = {
  45. User: [
  46. { name: '2cool', type: 'string' },
  47. { name: 'has space', type: 'string' },
  48. { name: 'speci@alChar', type: 'string' },
  49. { name: 'has-dash', type: 'string' },
  50. ],
  51. };
  52. const result = validateCustomFieldsConfig(config, allEntities);
  53. expect(result.valid).toBe(false);
  54. expect(result.errors).toEqual([
  55. 'User entity has an invalid custom field name: "2cool"',
  56. 'User entity has an invalid custom field name: "has space"',
  57. 'User entity has an invalid custom field name: "speci@alChar"',
  58. 'User entity has an invalid custom field name: "has-dash"',
  59. ]);
  60. });
  61. it('duplicate names', () => {
  62. const config: CustomFields = {
  63. User: [
  64. { name: 'foo', type: 'string' },
  65. { name: 'bar', type: 'string' },
  66. { name: 'baz', type: 'string' },
  67. { name: 'foo', type: 'boolean' },
  68. { name: 'bar', type: 'boolean' },
  69. ],
  70. };
  71. const result = validateCustomFieldsConfig(config, allEntities);
  72. expect(result.valid).toBe(false);
  73. expect(result.errors).toEqual([
  74. 'User entity has duplicated custom field name: "foo"',
  75. 'User entity has duplicated custom field name: "bar"',
  76. ]);
  77. });
  78. it('duplicate names in translation', () => {
  79. const config: CustomFields = {
  80. Product: [
  81. { name: 'foo', type: 'string' },
  82. { name: 'bar', type: 'string' },
  83. { name: 'baz', type: 'string' },
  84. { name: 'foo', type: 'localeString' },
  85. { name: 'bar', type: 'boolean' },
  86. ],
  87. };
  88. const result = validateCustomFieldsConfig(config, allEntities);
  89. expect(result.valid).toBe(false);
  90. expect(result.errors).toEqual([
  91. 'Product entity has duplicated custom field name: "foo"',
  92. 'Product entity has duplicated custom field name: "bar"',
  93. ]);
  94. });
  95. it('name conflict with existing fields', () => {
  96. const config: CustomFields = {
  97. Product: [{ name: 'createdAt', type: 'string' }],
  98. };
  99. const result = validateCustomFieldsConfig(config, allEntities);
  100. expect(result.valid).toBe(false);
  101. expect(result.errors).toEqual(['Product entity already has a field named "createdAt"']);
  102. });
  103. it('name conflict with existing fields in translation', () => {
  104. const config: CustomFields = {
  105. Product: [{ name: 'name', type: 'string' }],
  106. };
  107. const result = validateCustomFieldsConfig(config, allEntities);
  108. expect(result.valid).toBe(false);
  109. expect(result.errors).toEqual(['Product entity already has a field named "name"']);
  110. });
  111. it('non-nullable must have defaultValue', () => {
  112. const config: CustomFields = {
  113. Product: [{ name: 'foo', type: 'string', nullable: false }],
  114. };
  115. const result = validateCustomFieldsConfig(config, allEntities);
  116. expect(result.valid).toBe(false);
  117. expect(result.errors).toEqual([
  118. 'Product entity custom field "foo" is non-nullable and must have a defaultValue',
  119. ]);
  120. });
  121. });