When creating a model, I'd like to specify a pattern for my phoneNum property, so I have:
@property({
type: 'string',
jsonSchema: {
pattern: '\d{3}-\d{3}-\d{4}',
// pattern: '^[0-9()\\-\\.\\s]+$'
},
})
phoneNum?: string;
There are actually 2 problems:
Problem 1
If I have pattern: '\d{3}-\d{3}-\d{4}', and hit save, it turns into:
pattern: 'd{3}-d{3}-d{4}',
Notice the \ got removed. Not knowing a better solution, I disable prettier by adding *.ts in .prettierignore.
If it's within the quotes, should the string be treated as-is? (It's likely not a LB issue).
Problem 2
Given the pattern above, I was hoping it accepts 111-111-1111 as the phone number. But I got the following error in API Explorer:
{
"error": {
"statusCode": 422,
"name": "UnprocessableEntityError",
"message": "The request body is invalid. See error object `details` property for more info.",
"code": "VALIDATION_FAILED",
"details": [
{
"path": ".phoneNum",
"code": "pattern",
"message": "should match pattern \"d{3}-d{3}-d{4}$\"",
"info": {
"pattern": "d{3}-d{3}-d{4}$"
}
}
]
}
}
Also noticed the removed \.
I can verify that the AJV pattern is taking effect, because if i change the pattern to something else (e.g. '^[0-9()\\-\\.\\s]+$', it works fine. The problem happened when there's \d.
@raymondfeng @jannyHou , you are more familiar with AJV, do you know what could be the issue? Thanks.
pattern: 'd{3}-d{3}-d{4}',
IIUC, you are trying to create a regular string and use \d as an escape sequence similar to \n. I suspect that \d is not a valid sequence, therefore Prettier is converting it to d, because that's what the JavaScript runtime is going to do anyways. You can try this yourself:
$ node
> '\d{2}'
'd{2}'
I think you need to use double backslash as follows:
$ node
> new RegExp('\\d{2}')
/\d{2}/
Here is the fixed property definition:
@property({
type: 'string',
jsonSchema: {
pattern: '\\d{3}-\\d{3}-\\d{4}',
// pattern: '^[0-9()\\-\\.\\s]+$'
},
})
phoneNum?: string;
It would be nice if @property decorator supported regular expressions in jsonSchema.pattern field:
@property({
type: 'string',
jsonSchema: {
pattern: /\d{3}-\d{3}-\d{4}/,
},
})
phoneNum?: string;
Until then, you can use .source property of regular expressions.
pattern: /\d{3}-\d{3}-\d{4}/.source,
Thanks @bajtos . Double backslash works for me. i.e.
jsonSchema: {
pattern: '\\d{3}-\\d{3}-\\d{4}',
},
I created a new issue to improve UX around jsonSchema.pattern - see https://github.com/strongloop/loopback-next/issues/4228
Most helpful comment
IIUC, you are trying to create a regular string and use
\das an escape sequence similar to\n. I suspect that\dis not a valid sequence, therefore Prettier is converting it tod, because that's what the JavaScript runtime is going to do anyways. You can try this yourself:I think you need to use double backslash as follows:
Here is the fixed property definition:
It would be nice if
@propertydecorator supported regular expressions injsonSchema.patternfield:Until then, you can use
.sourceproperty of regular expressions.