service_configs_validator.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. var App = require('app');
  18. App.ServiceConfigsValidator = Em.Object.extend({
  19. /**
  20. * Defaults recommended for various properties. This is to be used
  21. * by this validator to validate a property. Generally this value should be
  22. * set to values given by the defaults provider of the service.
  23. *
  24. * The key is the property name, and the value is the recommended default.
  25. */
  26. recommendedDefaults: {},
  27. /**
  28. * Per property validators where key is config name and value
  29. * is the validation function. This field is expected to be
  30. * overridden by extending classes.
  31. */
  32. configValidators: {},
  33. /**
  34. * Validate the given config property with the available
  35. * {@param recommendedDefaults}. This can do cross-property
  36. * validations also.
  37. *
  38. * @param config {App.ServiceConfigProperty}
  39. * @return {string} No validation issues when <code>null</code> returned.
  40. */
  41. validateConfig: function(config) {
  42. var validatorFunction = this.get('configValidators')[config.get('name')];
  43. if (validatorFunction) {
  44. return this[validatorFunction](config);
  45. }
  46. return null;
  47. },
  48. /**
  49. * Check if provided <code>config.value</code> is less than <code>recommendedDefaults</code>
  50. * @param {object} config - configProperty name
  51. */
  52. validatorLessThenDefaultValue: function(config) {
  53. var defaultValue = this.get('recommendedDefaults')[config.get('name')];
  54. var currentValue = parseInt(config.get('value').toString().replace( /\D+/g, ''));
  55. if (!defaultValue) {
  56. return null;
  57. }
  58. defaultValue = parseInt(defaultValue.toString().replace( /\D+/g, ''));
  59. if (defaultValue && currentValue && currentValue < defaultValue) {
  60. return "Value is less than the recommended default of "+defaultValue;
  61. }
  62. return null;
  63. }
  64. });