-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(deployment-tool): DefaultInputValidator
Closes #1709
- Loading branch information
1 parent
6b342cb
commit c11ac1d
Showing
3 changed files
with
146 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
client/src/plugins/camunda-plugin/deployment-tool/validation/DefaultInputValidator.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH | ||
* under one or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information regarding copyright | ||
* ownership. | ||
* | ||
* Camunda licenses this file to you under the MIT; you may not use this file | ||
* except in compliance with the MIT License. | ||
*/ | ||
|
||
export default class DefaultInputValidator { | ||
|
||
// This validator validates input fields: | ||
// Not initially | ||
// Only when the form is submitted | ||
// | ||
// This validator is also "forgiving", it means that | ||
// once the user types anything on a non-validated field | ||
// the error dissapears and won't be shown until the form | ||
// is submitted again. | ||
|
||
constructor(validateNonEmpty, text) { | ||
this.validateNonEmpty = validateNonEmpty; | ||
this.text = text; | ||
} | ||
|
||
_validate(value, forceRecheck) { | ||
const { text, validateNonEmpty, cachedValidationResult } = this; | ||
|
||
if (forceRecheck) { | ||
const result = validateNonEmpty(value, text); | ||
this.cachedValidationResult = result; | ||
return result; | ||
} | ||
return cachedValidationResult; | ||
} | ||
|
||
validate(value, isOnBeforeSubmit) { | ||
|
||
// always force validation before submit | ||
if (isOnBeforeSubmit) { | ||
this.cachedValue = value; | ||
return this._validate(value, true); | ||
} | ||
|
||
// user is typing on the field | ||
if (value !== this.cachedValue) { | ||
this.cachedValue = value; | ||
this.cachedValidationResult = null; | ||
return null; | ||
} | ||
|
||
// user is not typing on the field. | ||
if (value === this.cachedValue) { | ||
return this._validate(value, false); | ||
} | ||
|
||
this.cachedValue = value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters