Use of pattern within JSON schema


I am attempting to create a pattern that can be used with a $jsonSchema for M036. Everything else is straight forward, but this:-

Pattern Problem

The problem is that the string must start with a $

  • “claims.amount” must be a string that begins with $

So outside of JSON Schema I have come up with

/^$[^ ].*/g

However when I try the

 amount: {
      bsonType: "string",
      pattern: "^\$[^ ].*"

It blocks both $100 and 100 as an amount. If I take away the pattern, I can insert strings.

Any help would be greatly appreciated.

NOTE - I tried the below and it worked. Any thoughts on why both worked in a regex simulator, but only one worked in $jsonSchema. 2.5 hours of my life I will not get back :slight_smile:

amount: {
   bsonType: "string",
   pattern: "^[$].*"

No one has any thoughts on this?

I did wonder if jsonSchema has a more limited regex syntax.

Hi @NeilM,

You need to double escape the $ when defining in jsonSchema. For example, "^\\$\\d+"


1 Like

Thanks for that, I did try it and it worked :slight_smile:

I went with: -

^\\$[^ ].*

Since it also handled decimal points in the currency, but ignored spaces e.g.

$ 180 = Would be rejected
$180.00 = Would be accepted.
$180 = Would be accepted.

Q. Do you need to double escape other things, apart from $ when using patterns in $jsonSchema?

Hi @NeilM,

Glad you got it working.

Any regex syntax that need to be a literal needs to be double escaped.

Let’s take your example, we would like specify a literal $ character, which is a regex syntax for boundary-type assertions indicating the end of an input . We would need escape the character with a single \ to be literal, however in jsonSchema, the \ would also needed to be escaped with another slash \.

Hope that helps.


1 Like


Thank you for that. To be fair an excellent description of the whys and wherefores of handling a literal, and the additional handling required for the JSON schema.

It actually means I more of an idea of what I should be doing now, instead of the bumbling around I was doing before.