projectSamplingForm.tsx 943 B

1234567891011121314151617181920212223242526272829303132333435
  1. import {t} from 'sentry/locale';
  2. import {createForm} from 'sentry/views/settings/dynamicSampling/utils/formContext';
  3. type FormFields = {
  4. projectRates: {[id: string]: string};
  5. };
  6. type FormErrors = {
  7. projectRates: Record<string, string>;
  8. };
  9. export const projectSamplingForm = createForm<FormFields, FormErrors>({
  10. validators: {
  11. projectRates: value => {
  12. const errors: Record<string, string> = {};
  13. Object.entries(value).forEach(([projectId, rate]) => {
  14. if (rate === '') {
  15. errors[projectId] = t('This field is required');
  16. }
  17. const numericRate = Number(rate);
  18. if (isNaN(numericRate)) {
  19. errors[projectId] = t('Please enter a valid number');
  20. }
  21. if (numericRate < 0 || numericRate > 100) {
  22. errors[projectId] = t('Must be between 0% and 100%');
  23. }
  24. });
  25. return Object.keys(errors).length === 0 ? undefined : errors;
  26. },
  27. },
  28. });