1

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?

2
  • 1
    The easiest way is to use parse+render wrapped in try/catch. If you can render the card its most likely valid :) Commented Apr 18, 2021 at 11:04
  • @TimCadenbach thank you for your comment, post it as a response in order to upvote you Commented Apr 18, 2021 at 13:39

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.