open-vault/ui/app/utils/build-api-validators.js
Arnav Palnitkar d1cc297cd9
Handle form validation for open api form (#11963)
* Handle form validation for open api form

- Added required validator for all the default fields

* Fixed field group error and adedd comments

* Fixed acceptance tests

* Added changelog

* Fix validation in edit mode

- Handle read only inputs during edit mode

* Minor improvements

* Restrict validation only for userpass
2021-07-13 15:50:27 -07:00

31 lines
898 B
JavaScript

import { validator, buildValidations } from 'ember-cp-validations';
/**
* Add validation on dynamic form fields generated via open api spec
* For fields grouped under default category, add the require/presence validator
* @param {Array} fieldGroups
* fieldGroups param example:
* [ { default: [{name: 'username'}, {name: 'password'}] },
* { Tokens: [{name: 'tokenBoundCidrs'}] }
* ]
* @returns ember cp validation class
*/
export default function initValidations(fieldGroups) {
let validators = {};
fieldGroups.forEach(element => {
if (element.default) {
element.default.forEach(v => {
validators[v.name] = createPresenceValidator(v.name);
});
}
});
return buildValidations(validators);
}
export const createPresenceValidator = function(label) {
return validator('presence', {
presence: true,
message: `${label} can't be blank.`,
});
};