Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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, validationOptions); // => {hasDeprecationWarnings: boolean, isValid: boolean}
  10. ```
  11. Where `ValidationOptions` are:
  12. ```ts
  13. type ValidationOptions = {
  14. comment?: string;
  15. condition?: (option: unknown, validOption: unknown) => boolean;
  16. deprecate?: (
  17. config: Record<string, unknown>,
  18. option: string,
  19. deprecatedOptions: DeprecatedOptions,
  20. options: ValidationOptions,
  21. ) => boolean;
  22. deprecatedConfig?: DeprecatedOptions;
  23. error?: (
  24. option: string,
  25. received: unknown,
  26. defaultValue: unknown,
  27. options: ValidationOptions,
  28. path?: Array<string>,
  29. ) => void;
  30. exampleConfig: Record<string, unknown>;
  31. recursive?: boolean;
  32. recursiveBlacklist?: Array<string>;
  33. recursiveDenylist?: Array<string>;
  34. title?: Title;
  35. unknown?: (
  36. config: Record<string, unknown>,
  37. exampleConfig: Record<string, unknown>,
  38. option: string,
  39. options: ValidationOptions,
  40. path?: Array<string>,
  41. ) => void;
  42. };
  43. type Title = {
  44. deprecation?: string;
  45. error?: string;
  46. warning?: string;
  47. };
  48. ```
  49. `exampleConfig` is the only option required.
  50. ## API
  51. 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:
  52. Almost anything can be overwritten to suite your needs.
  53. ### Options
  54. - `recursiveDenylist` – optional array of string keyPaths that should be excluded from deep (recursive) validation.
  55. - `comment` – optional string to be rendered below error/warning message.
  56. - `condition` – an optional function with validation condition.
  57. - `deprecate`, `error`, `unknown` – optional functions responsible for displaying warning and error messages.
  58. - `deprecatedConfig` – optional object with deprecated config keys.
  59. - `exampleConfig` – the only **required** option with configuration against which you'd like to test.
  60. - `recursive` - optional boolean determining whether recursively compare `exampleConfig` to `config` (default: `true`).
  61. - `title` – optional object of titles for errors and messages.
  62. You will find examples of `condition`, `deprecate`, `error`, `unknown`, and `deprecatedConfig` inside source of this repository, named respectively.
  63. ## exampleConfig syntax
  64. `exampleConfig` should be an object with key/value pairs that contain an example of a valid value for each key. A configuration value is considered valid when:
  65. - it matches the JavaScript type of the example value, e.g. `string`, `number`, `array`, `boolean`, `function`, or `object`
  66. - it is `null` or `undefined`
  67. - it matches the Javascript type of any of arguments passed to `MultipleValidOptions(...)`
  68. The last condition is a special syntax that allows validating where more than one type is permissible; see example below. It's acceptable to have multiple values of the same type in the example, so you can also use this syntax to provide more than one example. When a validation failure occurs, the error message will show all other values in the array as examples.
  69. ## Examples
  70. Minimal example:
  71. ```js
  72. validate(config, {exampleConfig});
  73. ```
  74. Example with slight modifications:
  75. ```js
  76. validate(config, {
  77. comment: ' Documentation: http://custom-docs.com',
  78. deprecatedConfig,
  79. exampleConfig,
  80. title: {
  81. deprecation: 'Custom Deprecation',
  82. // leaving 'error' and 'warning' as default
  83. },
  84. });
  85. ```
  86. This will output:
  87. #### Warning:
  88. ```bash
  89. ● Validation Warning:
  90. Unknown option transformx with value "<rootDir>/node_modules/babel-jest" was found.
  91. This is either a typing error or a user mistake. Fixing it will remove this message.
  92. Documentation: http://custom-docs.com
  93. ```
  94. #### Error:
  95. ```bash
  96. ● Validation Error:
  97. Option transform must be of type:
  98. object
  99. but instead received:
  100. string
  101. Example:
  102. {
  103. "transform": {
  104. "\\.js$": "<rootDir>/preprocessor.js"
  105. }
  106. }
  107. Documentation: http://custom-docs.com
  108. ```
  109. ## Example validating multiple types
  110. ```js
  111. import {multipleValidOptions} from 'jest-validate';
  112. validate(config, {
  113. // `bar` will accept either a string or a number
  114. bar: multipleValidOptions('string is ok', 2),
  115. });
  116. ```
  117. #### Error:
  118. ```bash
  119. ● Validation Error:
  120. Option foo must be of type:
  121. string or number
  122. but instead received:
  123. array
  124. Example:
  125. {
  126. "bar": "string is ok"
  127. }
  128. or
  129. {
  130. "bar": 2
  131. }
  132. Documentation: http://custom-docs.com
  133. ```
  134. #### Deprecation
  135. Based on `deprecatedConfig` object with proper deprecation messages. Note custom title:
  136. ```bash
  137. Custom Deprecation:
  138. Option scriptPreprocessor was replaced by transform, which support multiple preprocessors.
  139. Jest now treats your current configuration as:
  140. {
  141. "transform": {".*": "xxx"}
  142. }
  143. Please update your configuration.
  144. Documentation: http://custom-docs.com
  145. ```