25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

README.md 3.6 KiB

3 yıl önce
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. # jest-validate
  2. Generic configuration validation tool that helps you with warnings, errors and deprecation messages as well as showing users examples of correct configuration.
  3. ```bash
  4. npm install --save jest-validate
  5. ```
  6. ## Usage
  7. ```js
  8. import {validate} from 'jest-validate';
  9. validate((config: Object), (options: ValidationOptions)); // => {hasDeprecationWarnings: boolean, isValid: boolean}
  10. ```
  11. Where `ValidationOptions` are:
  12. ```js
  13. type ValidationOptions = {
  14. blacklist?: Array<string>,
  15. comment?: string,
  16. condition?: (option: any, validOption: any) => boolean,
  17. deprecate?: (
  18. config: Object,
  19. option: string,
  20. deprecatedOptions: Object,
  21. options: ValidationOptions,
  22. ) => true,
  23. deprecatedConfig?: {[key: string]: Function},
  24. error?: (
  25. option: string,
  26. received: any,
  27. defaultValue: any,
  28. options: ValidationOptions,
  29. ) => void,
  30. exampleConfig: Object,
  31. recursive?: boolean,
  32. title?: Title,
  33. unknown?: (
  34. config: Object,
  35. exampleConfig: Object,
  36. option: string,
  37. options: ValidationOptions,
  38. ) => void,
  39. };
  40. type Title = {|
  41. deprecation?: string,
  42. error?: string,
  43. warning?: string,
  44. |};
  45. ```
  46. `exampleConfig` is the only option required.
  47. ## API
  48. By default `jest-validate` will print generic warning and error messages. You can however customize this behavior by providing `options: ValidationOptions` object as a second argument:
  49. Almost anything can be overwritten to suite your needs.
  50. ### Options
  51. - `recursiveBlacklist` – optional array of string keyPaths that should be excluded from deep (recursive) validation.
  52. - `comment` – optional string to be rendered below error/warning message.
  53. - `condition` – an optional function with validation condition.
  54. - `deprecate`, `error`, `unknown` – optional functions responsible for displaying warning and error messages.
  55. - `deprecatedConfig` – optional object with deprecated config keys.
  56. - `exampleConfig` – the only **required** option with configuration against which you'd like to test.
  57. - `recursive` - optional boolean determining whether recursively compare `exampleConfig` to `config` (default: `true`).
  58. - `title` – optional object of titles for errors and messages.
  59. You will find examples of `condition`, `deprecate`, `error`, `unknown`, and `deprecatedConfig` inside source of this repository, named respectively.
  60. ## Examples
  61. Minimal example:
  62. ```js
  63. validate(config, {exampleConfig});
  64. ```
  65. Example with slight modifications:
  66. ```js
  67. validate(config, {
  68. comment: ' Documentation: http://custom-docs.com',
  69. deprecatedConfig,
  70. exampleConfig,
  71. title: {
  72. deprecation: 'Custom Deprecation',
  73. // leaving 'error' and 'warning' as default
  74. },
  75. });
  76. ```
  77. This will output:
  78. #### Warning:
  79. ```bash
  80. ● Validation Warning:
  81. Unknown option transformx with value "<rootDir>/node_modules/babel-jest" was found.
  82. This is either a typing error or a user mistake. Fixing it will remove this message.
  83. Documentation: http://custom-docs.com
  84. ```
  85. #### Error:
  86. ```bash
  87. ● Validation Error:
  88. Option transform must be of type:
  89. object
  90. but instead received:
  91. string
  92. Example:
  93. {
  94. "transform": {
  95. "^.+\\.js$": "<rootDir>/preprocessor.js"
  96. }
  97. }
  98. Documentation: http://custom-docs.com
  99. ```
  100. #### Deprecation
  101. Based on `deprecatedConfig` object with proper deprecation messages. Note custom title:
  102. ```bash
  103. Custom Deprecation:
  104. Option scriptPreprocessor was replaced by transform, which support multiple preprocessors.
  105. Jest now treats your current configuration as:
  106. {
  107. "transform": {".*": "xxx"}
  108. }
  109. Please update your configuration.
  110. Documentation: http://custom-docs.com
  111. ```