Files
teleport/web/packages/shared/components/Validation/Validation.jsx
T
Jeff Pihach 5eafe86fa4 Remove usage of lodash methods (#21567)
* Create new local util lib to replace lodash.

* Replace usage of isInteger and debounce from lodash with highbar.

* Create isObject and runOnce utility methods.

* remove use of at, isObject, and once lodash method usage.

* remove map and transform lodash calls.

* Add memoize function to highbar.

* remove memoize lodash usage.

* remove merge and isEqual lodash methods and update other missing refs to highbar.

* convert the throttle to debounce.

* add throttle method to highbar.

* use the new throttle method instead of debounce where necessary.

* Add mergeDeep function for init config merge.

* remove lodash from the build process.

* Fix introduced bug in workspacesService.

* Added tests for highbar mergeDeep and expanded its functionality to support arrays.

* review updates.

* Added types to mergeDeep function.

* Add missing MapCache prototype methods.

* Add license notices, types and missing hash code.

* First pass at compare an array objects function.

* use new compareArrayObjs fn

* Add missing not

* Added types to arrayObjectIsEqual

* Add tests for arrayObjectIsEqual and fix some edge case bugs.

* update util fn name
2023-02-21 17:25:36 +00:00

104 lines
2.3 KiB
React

/*
Copyright 2019 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import React from 'react';
import { isObject } from 'shared/utils/highbar';
import Logger from '../../libs/logger';
const logger = Logger.create('validation');
// Validator handles input validation
export default class Validator {
valid = true;
constructor() {
// store subscribers
this._subs = [];
}
// adds a callback to the list of subscribers
subscribe(cb) {
this._subs.push(cb);
}
// removes a callback from the list of subscribers
unsubscribe(cb) {
const index = this._subs.indexOf(cb);
if (index > -1) {
this._subs.splice(index, 1);
}
}
addResult(result) {
// result can be a boolean value or an object
let isValid = false;
if (isObject(result)) {
isValid = result.valid;
} else {
logger.error(`rule should return a valid object`);
}
this.valid = this.valid && Boolean(isValid);
}
reset() {
this.valid = true;
this.validating = false;
}
validate() {
this.reset();
this.validating = true;
this._subs.forEach(cb => {
try {
cb();
} catch (err) {
logger.error(err);
}
});
return this.valid;
}
}
const ValidationContext = React.createContext({});
export function Validation(props) {
const [validator] = React.useState(() => new Validator());
// handle render functions
const children =
typeof props.children === 'function'
? props.children({ validator })
: props.children;
return (
<ValidationContext.Provider value={validator}>
{children}
</ValidationContext.Provider>
);
}
export function useValidation() {
const value = React.useContext(ValidationContext);
if (!(value instanceof Validator)) {
logger.warn('Missing Validation Context declaration');
}
return value;
}