Skip to content

Commit

Permalink
added validations for 2nd page
Browse files Browse the repository at this point in the history
  • Loading branch information
lokesh-couchbase committed Oct 26, 2023
1 parent 1dfb694 commit c53dcf2
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 19 deletions.
80 changes: 69 additions & 11 deletions src/commands/tools/dataImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,15 +195,22 @@ export class DataImport {
}
} else if (this.fileFormat === this.CSV_FILE_FORMAT) {
const expression = keyExpr;
const pattern = new RegExp(this.WORDS_WITH_PERCENT_SYMBOLS_REGEX);
const matcher = expression.match(pattern);
const fieldNamesList: string[] = [];
const pattern = new RegExp(this.WORDS_WITH_PERCENT_SYMBOLS_REGEX, 'g');
let matches: string[] = [];

if (matcher) {
matcher.forEach(match => {
fieldNamesList.push(match.replace(/%/g, ''));
});
let match;

while ((match = pattern.exec(expression)) !== null) {
// match[1] contains the captured word (the part between % symbols)
if(match[1] === ""){
break;
}
matches.push(match[1]);
}
const fieldNamesList: string[] = [];
matches.forEach(match => {
fieldNamesList.push(match.replace(/%/g, ''));
});

for (let i = 0; i < Math.min(this.cachedCsvDocs.size, this.PREVIEW_SIZE); i++) {
let keyBuilder = expression;
Expand All @@ -223,6 +230,11 @@ export class DataImport {
previewContent.push(keyBuilder);
}
}
} else { // Random UUID case
for(let i = 0; i < this.PREVIEW_SIZE; i++) {
previewContent.push(uuidv4()); // Shows Random UUID, May not represent real values
}

}
return previewContent.join("\n");
}
Expand Down Expand Up @@ -468,7 +480,7 @@ export class DataImport {
return errors;
};

validateFormData = async (formData: any): Promise<string> => {
validateDatasetAndCollectionFormData = async (formData: any): Promise<string> => {
let errors: string[] = [];

// Validate Dataset
Expand Down Expand Up @@ -549,6 +561,46 @@ export class DataImport {
return "";
};

async validateKeysAndAdvancedSettingsFormData(formData: any): Promise<string>{
let errors: string[] = [];

// Validate Keys (No hard check here, we allow any data to pass as it will fail eventually)
if(formData.keyOptions === "fieldValue") {
if(!formData.keyFieldName || formData.keyFieldName.trim() === ""){
errors.push("Field name field is empty.");
}
} else if(formData.keyOptions === "customExpression") {
if(!formData.customExpression || formData.customExpression.trim() === ""){
errors.push("Custom expression field is empty.");
}
}

// Validate Advanced Settings
if(formData.skipDocsOrRows && String(formData.skipDocsOrRows)!== "" && parseInt(String(formData.skipDocsOrRows)) < 0) {
// If skipFirstDocuments exists but are less than 0
errors.push("Skip first field does not contain a valid non-negative integer.");
}

if(formData.limitDocsOrRows && String(formData.limitDocsOrRows)!== "" && parseInt(String(formData.limitDocsOrRows)) < 0) {
// If limitDocsOrRows exists but are less than 0
errors.push("Import up to field does not contain a valid non-negative integer.");
}

if(formData.ignoreFields && String(formData.ignoreFields).trim()!== "") { // Ignore fields exists, validating it
// TODO: Add validations for CSV File format
}

if(!formData.threads || !formData.threads.trim() || parseInt(formData.threads)<1){
errors.push("threads cannot be undefined or less than 1");
}

// Return the array of error messages
if (errors.length > 0) {
return errors.join("<br>");
}
return "";
}

public dataImport = async () => {
const connection = getActiveConnection();
if (!connection) {
Expand Down Expand Up @@ -618,8 +670,8 @@ export class DataImport {
case "vscode-couchbase.tools.dataImport.runImport":
const runFormData = message.data;
const datasetAndCollectionData = message.datasetAndCollectionData;
const runValidationError = await this.validateFormData(runFormData);
if (runValidationError === "" || true) {
const runValidationError = await this.validateKeysAndAdvancedSettingsFormData(runFormData);
if (runValidationError === "" ) {
CBImport.import({
bucket: datasetAndCollectionData.bucket, // TODO: bucket should be taken from other form
dataset: datasetAndCollectionData.dataset,
Expand All @@ -634,12 +686,18 @@ export class DataImport {
threads: runFormData.threads,
verbose: runFormData.verbose,
});
} else {
currentPanel.webview.postMessage({
command:
"vscode-couchbase.tools.dataImport.getKeysAndAdvancedSettingsPageFormValidationError",
error: runValidationError,
});
}

break;
case "vscode-couchbase.tools.dataImport.nextGetDatasetAndCollectionPage":
const formData = message.data;
const validationError = await this.validateFormData(formData);
const validationError = await this.validateDatasetAndCollectionFormData(formData);
if (validationError === "") {
// NO Validation Error on Page 1, We can shift to next page
currentPanel.webview.html = getLoader("Data Import");
Expand Down
2 changes: 1 addition & 1 deletion src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ export function activate(context: vscode.ExtensionContext) {
vscode.commands.registerCommand(
Commands.dataImport,
async () => {
await new DataImport('').dataImport();
await new DataImport().dataImport();
}
)
);
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Tools/DataExport/dataExport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ const validateFormData = (formData: any): string => {
errors.push("Please inform the file destination folder");
}

if(!formData.threads.trim() || parseInt(formData.threads)<1){
if(!formData.threads || !formData.threads.trim() || parseInt(formData.threads)<1){
errors.push("threads cannot be undefined or less than 1");
}

Expand Down
8 changes: 4 additions & 4 deletions src/tools/CBImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,18 +108,18 @@ export class CBImport {

// Advanced Settings
if(importData.skipDocsOrRows && importData.skipDocsOrRows.trim() !== ""){
if(importData.fileFormat === "JSON") {
if(importData.fileFormat === "json") {
cmd.push("--skip-docs");
} else if(importData.fileFormat === "CSV") {
} else if(importData.fileFormat === "csv") {
cmd.push("--skip-rows");
}
cmd.push(importData.skipDocsOrRows);
}

if(importData.limitDocsOrRows && importData.limitDocsOrRows.trim() !== ""){
if(importData.fileFormat === "JSON") {
if(importData.fileFormat === "json") {
cmd.push("--limit-docs");
} else if(importData.fileFormat === "CSV") {
} else if(importData.fileFormat === "csv") {
cmd.push("--limit-rows");
}
cmd.push(importData.limitDocsOrRows);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,11 @@ export const getKeysAndAdvancedSettings = (lastPageData: any): string => {
<div class="advanced-settings" id="advanced-settings">
<label for="skipFirstDocuments">Skip the first # Documents:</label>
<input type="text" name="skipFirstDocuments" id="skipFirstDocuments" value="">
<input type="number" name="skipFirstDocuments" id="skipFirstDocuments" value="">
<br>
<label for="importUptoDocuments">Import up to # Documents:</label>
<input type="text" name="importUptoDocuments" id="importUptoDocuments" value="">
<input type="number" name="importUptoDocuments" id="importUptoDocuments" value="">
<br>
<label for="ignoreFields">Ignore the fields:</label>
Expand Down Expand Up @@ -353,6 +353,9 @@ export const getKeysAndAdvancedSettings = (lastPageData: any): string => {
let preview = message.preview;
document.getElementById("keyPreviewTextArea").innerHTML = preview;
break;
case "vscode-couchbase.tools.dataImport.getKeysAndAdvancedSettingsPageFormValidationError":
let error = message.error;
document.getElementById("validation-error").innerHTML = error;
}
})
</script>
Expand Down

0 comments on commit c53dcf2

Please sign in to comment.