Skip to content

Commit

Permalink
feat(ember): added validator that validates field based on another field
Browse files Browse the repository at this point in the history
  • Loading branch information
c0rydoras committed Feb 19, 2024
1 parent 62000af commit b79f463
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions ember/app/utils/validations-when-other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import validateWhenOther from 'outdated/validators/validate-when-other';

const validationsWhenOther = ({
field,
otherFieldValidator,
fieldValidators,
}) =>
fieldValidators.map((fv) =>
validateWhenOther({
field,
otherFieldValidator,
fieldValidator: fv,
}),
);
export default validationsWhenOther;
13 changes: 13 additions & 0 deletions ember/app/validators/validate-when-other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { get } from '@ember/object';

export default function validateWhenOther(options = {}) {
return (key, value, oldValue, changes, content) => {
const { field, otherFieldValidator, fieldValidator } = options;
const fieldValue = get(changes, field) || get(content, field);

const result = otherFieldValidator(field, fieldValue, null, null, null);
if (result !== true) return true;

return fieldValidator(key, value, oldValue, changes, content);
};
}

0 comments on commit b79f463

Please sign in to comment.