Validating adaptive card inputs

Viewed 138

I have an adaptive card to get username and password , i need to validate them using the data stored in my cosmos db container. is that possible? suggestions would be appreciated

here is my adaptive card json

{
    "$schema": "http://adaptivecards.io/schemas/adaptivecard.json",
    "type": "AdaptiveCard",
    "version": "1.3",
    "actions": [
        {
            "type": "Action.Submit",
            "title": "Sign-up",
            "data": "${signin}",
            "id": "7"
        },
        {
            "type": "Action.Submit",
            "title": "Password Reset",
            "data": "${preset}",
            "id": "8"
        }
    ],
    "body": [
        {
            "type": "TextBlock",
            "wrap": true,
            "text": "Menu",
            "style": "heading",
            "fontType": "Default",
            "color": "Good",
            "weight": "Bolder",
            "id": "1"
        },
        {
            "type": "ActionSet",
            "actions": [
                {
                    "type": "Action.ShowCard",
                    "title": "Sign-in",
                    "card": {
                        "type": "AdaptiveCard",
                        "body": [
                            {
                                "type": "TextBlock",
                                "text": "Username",
                                "label": "Your name (Last, First)",
                                "validation": "^[A-Z][a-z]+, [A-Z][a-z]+$",
                                "data": "${username}",
                                "wrap": true,
                                "id": "3"
                            },
                            {
                                "type": "Input.Text",
                                "placeholder": "Lastname,firstname",
                                "id": "4"
                            },
                            {
                                "type": "TextBlock",
                                "text": "Password",
                                "wrap": true,
                                "id": "5"
                            },
                            {
                                "type": "Input.Text",
                                "placeholder": "***********",
                                "id": "6"
                            }
                        ]
                    },
                    "id": "2"
                }
            ]
        }
    ]
}
      

how do i validate the user inputs with cosmos db, what are the methods and functions to be created.

1 Answers

Yes, it's possible, you can use the @azure/cosmos npm package, but I think you'd need to handle the validation after submission of the adaptive card. I've done queries in my bot using the method below. Might be a better way of doing it for passwords.

const { CosmosClient } = require("@azure/cosmos");

const db = process.env.CosmosDbDatabaseId;
const endpoint = process.env.CosmosDbEndpoint;
const key = process.env.CosmosDbAuthKey;
const client = new CosmosClient({
  endpoint,
  key,
});
  try {
// Set up the query
const queryUserPrincipalName = YOUR-ADAPTIVE-CARD-USERNAME-FIELD;
const query = `SELECT * FROM c WHERE c.Username = "${queryUserPrincipalName}"`;

// Connect and query the container
try {
  // Connect to the container
  const userpasscontainer = client.database(db).container("YOUR-AZURE-COSMOS-CONTAINER");

  // Query the container
  const queryContainer = await userpasscontainer.items
    .query(query)
    .fetchAll();

  // Output the results from the query
  var queryUserPassItem = queryContainer.resources;
} catch (error) {
  // Write error to the console log
  console.log(`Error: ${error.message}`);
}

// Validate the username password
if($queryUserPassItem.Password === YOUR-ADAPTIVE-CARD-PASSWORD-FIELD) {
// Do stuff
}
Related