租房小程序前端代码
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.

27 lines
765 B

3 months ago
  1. const checkConfigMap = {
  2. endpoint: checkEndpoint,
  3. region: /^[a-zA-Z0-9\-_]+$/
  4. };
  5. function checkEndpoint(endpoint) {
  6. if (typeof endpoint === 'string') {
  7. return /^[a-zA-Z0-9._:/-]+$/.test(endpoint);
  8. } else if (endpoint.host) {
  9. return /^[a-zA-Z0-9._:/-]+$/.test(endpoint.host);
  10. }
  11. return false;
  12. }
  13. export const checkConfigValid = (conf, key: 'endpoint' | 'region'): void => {
  14. if (checkConfigMap[key]) {
  15. let isConfigValid = true;
  16. if (checkConfigMap[key] instanceof Function) {
  17. isConfigValid = (checkConfigMap[key] as Function)(conf);
  18. } else {
  19. isConfigValid = (checkConfigMap[key] as RegExp).test(conf);
  20. }
  21. if (!isConfigValid) {
  22. throw new Error(`The ${key} must be conform to the specifications`);
  23. }
  24. }
  25. };