How to validate CSV file whether it is in correct format or not ?

Hi Everyone,

I want implement a validation on file upload field, whenever user uploads a CSV file system will validate and check whether the CSV file uploaded is in correct format or not ?

if file is not in correct format system will give the error "File is not in correct format"

  Discussion posts and replies are publicly visible

  • Hi Sachin,
    You can add the following code in validations parameter of a!fileUploadfield() that will check where the file is in .csv format 
    code:

    validations: {
    a!localVariables(
    local!invalidExtensions: difference(
    upper(fv!files.extension),
    { "CSV"}
    ),

    if(
    length(local!invalidExtensions) > 0,
    "Please upload supported file formats: CSV",
    ""
    )
    )
    }

  • 0
    Certified Senior Developer

    Hello  

    What do you mean format? Do you mean the file extension type? If so below the code that would help.

    {
      a!fileUploadField(
        label: "File Upload",
        labelPosition: "ABOVE",
        target: cons!FOLDER_CONSTANT,
        saveInto: ri!file,
        value: ri!file,
        validations: a!localVariables(
          local!invalidExtensions: difference(upper(fv!files.extension), "CSV"),
          if(
            length(local!invalidExtensions) > 0,
            "Attachments must be pdf files. Remove: " & index(
              fv!files,
              "name",
              wherecontains(
                local!invalidExtensions,
                upper(fv!files.extension)
              ),
              {}
            ),
            ""
          )
        )
      )
    }

    Validation from documentation. Please refer THIS for more information

    docs.appian.com/.../File_Upload_Component.html

  • Typically when uploading an processing a CSV file (unless it is very small) we use an asynchronous pattern. The file is uploaded and the contents loaded to a staging table. The contents can then be validated and then, if valid, the data moved to the target operational table(s).

    In terms of implementing the actual validation, you can use JSON Schema Validation for the vast majority of the typical validations that you might want to apply.

    Use the JSON Tools plug-in the conduct JSON Schema validation (obviously you'll need convert your data in your staging table to JSON). If the volumes are very large you'll probably want to throttle the processing so as to not consume too much server resource in one go. If there are any Schema violations you'll need to write these somewhere. Whether the validation passes or fails you'll need to inform the original user since the processing is conducted asynchronously form the initial file upload.

    Visit JSON Schema Org for help with JSON Schemas