Validation function

Run Settings
LanguageJavaScript
Language Version
Run Command
const ERROR_MESSAGES = { requried:'Required', short: 'Too short', long: 'Too long' }; const formConfig = { title: { type: 'text', requried: true, minLength: 3 }, text: { type: 'text' } }; const errorFields = {}; const validate = (fields)=> { Object.keys(fields).forEach((key, idx) => { const value = fields[key]; if (!!formConfig[key].requried && !value.length) { errorFields[key] = [...errorFields[key], ERROR_MESSAGES.requried]; } if (!!formConfig[key].minLength && value.length <= formConfig[key].minLength) { errorFields[key] = [...errorFields[key], ERROR_MESSAGES.short]; } if (!!formConfig[key].maxLength && value.length > formConfig[key].maxLength) { errorFields[key] = [...errorFields[key], ERROR_MESSAGES.long]; } }); // return errorFields; console.log(errorFields); return !Boolean(Object.values(errorFields).filter(errors => errors.length).length); }; const fields = ({title: '', text: 'aaaaaaa'}); Object.keys(formConfig).forEach(key => errorFields[key] = []); const result = validate(fields); console.log({result});
Editor Settings
Theme
Key bindings
Full width
Lines