Acepting only two types of extensions

I need help setting up the code to accept only two extensions. I got the below code from Appian. This way, it only accepts one, either PNG or JPG. How do I switch this so it can be accepting of PNG and JPG? 

validations:
a!localVariables( local!invalidExtensions: difference(upper(fv!files.extension), { "PNG", "JPG" }), if( length(local!invalidExtensions) > 0, "Attachments must be images. Remove: " & index(fv!files, "name", wherecontains(local!invalidExtensions, upper(fv!files.extension)), {}), "" ) )

  Discussion posts and replies are publicly visible

Parents
  • +1
    Certified Lead Developer

    The requirement to have it spit out the list of files with invalid extensions makes it a little convoluted - a simpler validation message would be much easier to code and understand, FWIW.

    Anyway -- first off, there's a mistake in your index() function - you have 4 parameters (fv!files, then name, then your wherecontains(), then an empty set); so this wouldn't work under any circumstances.  The correct way of writing this would be index(fv!files.name, ...), which I believe is safe because the only time the validation message would attempt to evaluate, fv!files would have to be populated (and therefore have the .name property).

    Overall, though, my suggestion for you (after fixing the index() function) is to front-load a bit more of your logic into local variables so that they're understandable - in particular, the wherecontains() call.  If we calculate the indices of the bad extensions ahead of time, the later index() function becomes a lot more easy to understand.  For example:

    a!localVariables(
      local!uploadedDoc,
      
      a!fileUploadField(
        value: local!uploadedDoc,
        saveInto: local!uploadedDoc,
        validations: a!localVariables(
          local!allowedExtensions: {"png", "jpg"},
    
          local!invalidExtensions: difference(upper(fv!files.extension), { "PNG", "JPG" }),
          
          local!badExtensionIndices: wherecontains(
            local!invalidExtensions,
            upper(fv!files.extension)
          ),
    
          if(
            not(rule!APN_isEmpty(local!invalidExtensions)), 
            "Attachments must be images. Remove: " &
            index(
              fv!files.name, /* this was the main error before */
              local!badExtensionIndices,
              {}
            ), 
            null()
          )
        )
      )
    )

    Let me know if you have any questions.

Reply
  • +1
    Certified Lead Developer

    The requirement to have it spit out the list of files with invalid extensions makes it a little convoluted - a simpler validation message would be much easier to code and understand, FWIW.

    Anyway -- first off, there's a mistake in your index() function - you have 4 parameters (fv!files, then name, then your wherecontains(), then an empty set); so this wouldn't work under any circumstances.  The correct way of writing this would be index(fv!files.name, ...), which I believe is safe because the only time the validation message would attempt to evaluate, fv!files would have to be populated (and therefore have the .name property).

    Overall, though, my suggestion for you (after fixing the index() function) is to front-load a bit more of your logic into local variables so that they're understandable - in particular, the wherecontains() call.  If we calculate the indices of the bad extensions ahead of time, the later index() function becomes a lot more easy to understand.  For example:

    a!localVariables(
      local!uploadedDoc,
      
      a!fileUploadField(
        value: local!uploadedDoc,
        saveInto: local!uploadedDoc,
        validations: a!localVariables(
          local!allowedExtensions: {"png", "jpg"},
    
          local!invalidExtensions: difference(upper(fv!files.extension), { "PNG", "JPG" }),
          
          local!badExtensionIndices: wherecontains(
            local!invalidExtensions,
            upper(fv!files.extension)
          ),
    
          if(
            not(rule!APN_isEmpty(local!invalidExtensions)), 
            "Attachments must be images. Remove: " &
            index(
              fv!files.name, /* this was the main error before */
              local!badExtensionIndices,
              {}
            ), 
            null()
          )
        )
      )
    )

    Let me know if you have any questions.

Children