-
Notifications
You must be signed in to change notification settings - Fork 69
Add Path Validation for ShapefileSource and ImageSource.
#362
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
martinRenou
merged 6 commits into
geojupyter:main
from
Meriem-BenIsmail:path-validation
Jan 20, 2025
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5cfe38e
path validation for shapefile and image.
Meriem-BenIsmail ad4e9a7
make geojson inherit from pathbasedform
Meriem-BenIsmail 91f99d8
lint
Meriem-BenIsmail 4c1305e
lint check
Meriem-BenIsmail b89e7d0
add sourceType property to BaseForm.
Meriem-BenIsmail 93c2772
Merge branch 'main' into path-validation
Meriem-BenIsmail File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
119 changes: 119 additions & 0 deletions
119
packages/base/src/formbuilder/objectform/pathbasedsource.ts
This file contains hidden or 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,119 @@ | ||
| import { IDict, SourceType } from '@jupytergis/schema'; | ||
| import { showErrorMessage } from '@jupyterlab/apputils'; | ||
| import { IChangeEvent, ISubmitEvent } from '@rjsf/core'; | ||
|
|
||
| import { BaseForm, IBaseFormProps } from './baseform'; | ||
| import { loadFile } from '../../tools'; | ||
| import { FileSelectorWidget } from './fileselectorwidget'; | ||
|
|
||
| /** | ||
| * The form to modify a PathBasedSource source. | ||
| */ | ||
| export class PathBasedSourcePropertiesForm extends BaseForm { | ||
Meriem-BenIsmail marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| protected _sourceType: SourceType; | ||
|
|
||
| constructor(props: IBaseFormProps) { | ||
| super(props); | ||
| this._sourceType = ( | ||
| this.constructor as typeof BaseForm & { sourceType: SourceType } | ||
| ).sourceType; | ||
| if (this._sourceType !== 'GeoJSONSource') { | ||
| this._validatePath(props.sourceData?.path ?? ''); | ||
| } | ||
| } | ||
|
|
||
| protected processSchema( | ||
| data: IDict<any> | undefined, | ||
| schema: IDict, | ||
| uiSchema: IDict | ||
| ) { | ||
| super.processSchema(data, schema, uiSchema); | ||
| if (!schema.properties || !data) { | ||
| return; | ||
| } | ||
|
|
||
| // Customize the widget for path field | ||
| if (schema.properties && schema.properties.path) { | ||
| const docManager = | ||
| this.props.formChangedSignal?.sender.props.formSchemaRegistry.getDocManager(); | ||
|
|
||
| uiSchema.path = { | ||
| 'ui:widget': FileSelectorWidget, | ||
| 'ui:options': { | ||
| docManager, | ||
| formOptions: this.props | ||
| } | ||
| }; | ||
| } | ||
| // This is not user-editable | ||
| delete schema.properties.valid; | ||
| } | ||
|
|
||
| protected onFormBlur(id: string, value: any) { | ||
| // Is there a better way to spot the path text entry? | ||
| if (!id.endsWith('_path')) { | ||
| return; | ||
| } | ||
| this._validatePath(value); | ||
| } | ||
|
|
||
| // we need to use `onFormChange` instead of `onFormBlur` because it's no longer a text field | ||
| protected onFormChange(e: IChangeEvent): void { | ||
| super.onFormChange(e); | ||
| if (e.formData?.path !== undefined) { | ||
| this._validatePath(e.formData.path); | ||
| } | ||
| } | ||
|
|
||
| protected onFormSubmit(e: ISubmitEvent<any>) { | ||
| if (this.state.extraErrors?.path?.__errors?.length >= 1) { | ||
| showErrorMessage('Invalid file', this.state.extraErrors.path.__errors[0]); | ||
| return; | ||
| } | ||
| super.onFormSubmit(e); | ||
| } | ||
|
|
||
| /** | ||
| * Validate the path, to avoid invalid path. | ||
| * | ||
| * @param path - the path to validate. | ||
| */ | ||
| protected async _validatePath(path: string) { | ||
| const extraErrors: IDict = this.state.extraErrors; | ||
|
|
||
| let error = ''; | ||
| let valid = true; | ||
| if (!path) { | ||
| valid = false; | ||
| error = 'Path is required'; | ||
| } else { | ||
| try { | ||
| await loadFile({ | ||
| filepath: path, | ||
| type: this._sourceType, | ||
| model: this.props.model | ||
| }); | ||
| } catch (e) { | ||
| valid = false; | ||
| error = `"${path}" is not a valid ${this._sourceType} file.`; | ||
| } | ||
| } | ||
|
|
||
| if (!valid) { | ||
| extraErrors.path = { | ||
| __errors: [error] | ||
| }; | ||
|
|
||
| this.setState(old => ({ ...old, extraErrors })); | ||
| } else { | ||
| this.setState(old => ({ | ||
| ...old, | ||
| extraErrors: { ...extraErrors, path: { __errors: [] } } | ||
| })); | ||
| } | ||
|
|
||
| if (this.props.formErrorSignal) { | ||
| this.props.formErrorSignal.emit(!valid); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.