1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | 7x 18x 48x 17x 17x 17x 4x 13x 4x 9x 14x 2x 8x 6x 2x 2x 101x 101x 8x 101x 4x 4x 8x 4x 4x 8x 2x 4x 4x 4x 4x 30x 30x 30x 30x 30x 63x 30x 63x 63x 126x 64x 62x 33x 63x 63x 63x 33x 33x 33x 4x 4x 4x 5x 1x 1x 4x 4x 3x 4x 33x 33x 63x 63x 59x 59x 4x 4x 4x 4x 4x 63x 63x 63x 63x 33x 33x 30x 63x | // @ts-check /* eslint-disable no-param-reassign */ // This file contain any `private` method for the index.js import is from 'is_js'; import handlerMatcher from './matchers'; export const NAME_PLACEHOLDER = '#{NAME}#'; /** * Return error message for checking the parameters of the constructor. * * @export * @param {string} paramName * @param {any} value * @returns {string} */ export function getConstructorErrorMessage(paramName, value) { return `[Form Validation - ${paramName}] Expect: non empty object. Actual: ${value}`; } /** * Check if an object is a non-empty object * * @param {any} obj * @returns {boolean} */ function isNonEmptyObject(obj) { return is.object(obj) && is.not.empty(obj); } /** * Type check for 2 parameters of the constructor * * @export * @param {object} component * @param {object} schema */ export function typeCheck(component, schema) { const isComponentValid = isNonEmptyObject(component); const isSchemaValid = isNonEmptyObject(schema); if (!isComponentValid) { throw new Error( getConstructorErrorMessage('Parameter component', component) ); } if (!isSchemaValid) { throw new Error(getConstructorErrorMessage('Parameter schema', schema)); } Object.keys(schema).forEach(prop => { if (!isNonEmptyObject(schema[prop])) { throw new Error( getConstructorErrorMessage(`schema.${prop}`, schema[prop]) ); } }); } /** * Create initial value for a field if no default is provided. * * @export * @param {object} schema * @returns {boolean | string} */ export function createInitialValue(schema) { if (is.propertyDefined(schema, 'default')) { return schema.default; } Iif (is.propertyDefined(schema, 'min') || is.propertyDefined(schema, 'max')) { return '0'; } return ''; } /** * Create a new state for a field in componentState.formStatus.fields.field * * @export * @param {boolean} [needValue=false] * @param {object} [fieldSchema] * @returns {object} */ export function createNewFieldState(needValue = false, fieldSchema) { const result = { status: 'normal', errorText: '' }; if (needValue) { result.value = createInitialValue(fieldSchema); } return result; } export function createInitialState(schema, userState) { const initialState = { isFormOK: false, ...userState }; Object.keys(schema).forEach(prop => { initialState[prop] = createNewFieldState(true, schema[prop]); }); const schemaItems = Object.keys(schema); schemaItems.forEach(name => { if (is.propertyDefined(userState, name)) { initialState[name] = { ...initialState[name], ...userState[name] }; } }); return initialState; } /** * Check if we should change the state or not. * * @export * @param {object} oldState * @param {object} newState * @returns {boolean} */ export function shouldChange(oldState, newState) { const isErrorDifferent = oldState.status !== newState.status; const isValueDifferent = oldState.value !== newState.value; return isErrorDifferent || isValueDifferent; } /** * throw an error with defined text, usually calls by handler. * * @export * @param {any} value * @param {string} errorText * @returns {never} */ export function throwError(value, errorText) { const result = createNewFieldState(); result.value = value; result.status = 'error'; result.errorText = errorText; throw result; } /** * Modify the state from { state } to { fieldName: state } * * @export * @param {string} name * @param {object} result * @returns {object} */ export function addNameToResult(name, result) { if (result.status === 'error') { result.errorText = result.errorText.replace(NAME_PLACEHOLDER, name); } return { [name]: result }; } /** * It will run through the user's settings for a field, * and try matching to the matchers.js, * if according handler could be found, * it will then execute the according handler function. * For instance: * if user sets a `minLength` for a field, * This function will invoke the minLengthHandler() * * @param {object} matcher * @param {object} fieldState * @param {object} schema */ function runMatchers(matcher, fieldState, schema) { Object.keys(schema).forEach(ruleInSchema => { if (is.propertyDefined(matcher, ruleInSchema)) { matcher[ruleInSchema](fieldState.value, schema); } else Iif (ruleInSchema !== 'default') { console.warn(`No such rule: ${ruleInSchema}`); } }); return fieldState; } /** * This is the main entry for all validator. * * @export * @param {any} value * @param {object} schema * @returns {object} */ export function validatorRunner(value, schema) { const fieldState = createNewFieldState(); fieldState.value = value; return runMatchers(handlerMatcher, fieldState, schema); } /** * If field's status === `error` and its value isn't empty * we will set its status to `ok` * then return it. * It's fine to mutate here, since it's already a new state here. * * @export * @param {object} fieldState * @returns {object} */ export function checkFieldIsOK(fieldState) { Eif ( fieldState.status !== 'error' && is.existy(fieldState.value) && is.not.empty(fieldState.value) ) { fieldState.status = 'ok'; } return fieldState; } /** * If all fields in the state has their status !== error * Then we will set the isFormOK to true then return the state. * Just mutate the value since it's already a new state object * * @export * @param {object} componentState * @returns {object}} */ export function checkIsFormOK(schema, componentState) { const properties = Object.keys(schema); let isError = false; properties.some(prop => { if (componentState[prop].status === 'error') { isError = true; return true; } return false; }); if (!isError) { componentState.isFormOK = true; } return componentState; } export function restoreErrorStatus(fieldState) { Iif (fieldState.status === 'error' && is.empty(fieldState.value)) { fieldState.status = 'normal'; fieldState.errorText = ''; } return fieldState; } function updateWhenNeeded( newFieldState, propName, update, schema, formStatus = '' ) { const fieldState = addNameToResult(propName, newFieldState); if (formStatus === '') { update(fieldState); return } const oldFieldState = formStatus[propName]; const newFieldState1 = { ...oldFieldState, ...fieldState[propName] }; Iif (!shouldChange(oldFieldState, newFieldState)) { return; } const finalState = { ...formStatus, [propName]: { ...newFieldState1 } }; update(checkIsFormOK(schema, finalState)); } export function startValidating(target, schema, update, allState) { const propName = target.name; const fieldInfo = { value: target.value, schema: schema[propName] }; return Promise.resolve(fieldInfo) .then(info => validatorRunner(info.value, info.schema)) .then(result1 => checkFieldIsOK(result1)) .then(result2 => restoreErrorStatus(result2)) .catch(errorState => errorState) .then(newFieldState => updateWhenNeeded(newFieldState, propName, update, schema, allState) ); } |