Search code examples
javascriptadaptive-cards

how to validate adaptive card json using the javascript sdk


I am generating an adaptive card json on my own, and I want to validate if that json is well written according to the adaptive card schema. I am reading this docs and from there I have that there is a parse function.

I used it this way:

import * as AdaptiveCards from 'adaptivecards';

...

try {
  const adaptiveCard = new AdaptiveCards.AdaptiveCard();
  adaptiveCard.parse(json);
} catch (e) {
  console.log('Error', e);
}

this approach didn't seem to be working, since the parse accepts everything I pass into it as long as it is a json.

Also I tried this:

const v = adaptiveCard.validateProperties();

but the response is always the same, no matter if the schema is correct or not.

enter image description here

I mean, no matter if I have this:

{
  "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
  "type": "AdaptiveCard",
  "version": "1.3",
  "body": [
  ]
}

or:

{
      "schema": "http://adaptivecards.io/schemas/adaptive-card.json",
      "type": "AdaptiveCard",
      "version": "1.3",
      "boy": [
        {"catcher": true}
      ]
    }

or this:

    {
      "schema": "http://adaptivecards.io/schemas/adaptive-card.json",
      "type": "AdaptiveCard",
      "version": "1.3",
      "body": [
      {
         "type": "Container"
      }
    ]
  }

it always returns the same, even if there are unknown properties, or the Container has no items.

Maybe I am doing this the wrong way.

Any hint on how to validate this?


Solution

  • The easiest way to validate a card is to try to render it. There's a validate function but that doesn't really help and isn't finished.

    try this:

    var renderedCard = undefined;
    try {
      const adaptiveCard = new AdaptiveCards.AdaptiveCard();
      adaptiveCard.parse(json);
      renderedCard = adaptiveCard.render();
    } catch (e) {
      console.log('Error', e);
      return false;
    }

    Any valid card will not throw an exception, that doesn't mean that the card is fully working but at least the json code is valid for sure.