I have defined the property in my model as follows:
@property({
type: 'string',
required: true,
min: 2,
max: 255
})
name: string
However, when I send the field consisting of 1 character, it didn't throw me the error. Can anybody help me on this?
I have defined the property in my model as follows:
@property({
type: 'string',
required: true,
min: 2,
max: 255
})
name: string
However, when I send the field consisting of 1 character, it didn't throw me the error. Can anybody help me on this?
Share Improve this question asked Feb 22, 2019 at 17:27 AxelAxel 5,16118 gold badges77 silver badges150 bronze badges 7- Regarding your deleted question, see stackoverflow./questions/27626017/… – Wiktor Stribiżew Commented Feb 24, 2019 at 9:16
- @WiktorStribiżew Thanks man! It got one hell of a downvotes so I thought I asked a stupid question and deleted it. – Axel Commented Feb 24, 2019 at 9:17
-
You just need to understand what regex escape is. It seems you may use
RegExp('^(([^<>()[\\].,;:\\s@"]+(\\.[^<>()[\\].,;:\\s@"]+)*)|(".+"))@(([^<>()[\\].,;:\\s@"]+\\.)+[^<>()[\\].,;:\\s@"]{2,})$', 'i')
. Note the single quotes around the string literal (no need to single-escape double quotes) and double backslashes where single ones were. I removed unnecessary escapes (.
and[
inside character classes are literal chars, no special ones). – Wiktor Stribiżew Commented Feb 24, 2019 at 9:22 - Wow!!! Thank you so so so much man. – Axel Commented Feb 24, 2019 at 9:24
- I will undelete the question. It would be awesome if you post this answer there. Maybe it would be helpful to some people. stackoverflow./questions/54850216/… – Axel Commented Feb 24, 2019 at 9:27
2 Answers
Reset to default 3It should be like below,
@property({
type: 'string',
required: true,
jsonSchema: {
maxLength: 30,
minLength: 10,
},
})
name: string
You can refer the doc here
Loopback 4 (as well as Loopback 3) does not support min/max properties by default and would not use it for validation of the data you send to the API. You can see the list of supported property properties in the documentation for Loopback 3 as nothing has changed in Loopback 4.
Below is a statement from the Loopback 4 documentation:
The data from request body is validated against its OpenAPI schema specification. We use AJV module to perform the validation, which validates data with a JSON schema generated from the OpenAPI schema specification.
From Open API V3 documentation we can see they support string
data type and
String length can be restricted using minLength and maxLength:
AJV support minLength and maxLength properties too, but for some reason, Loopback 4 does not have an easy, builtin way to define these properties with the @property decorator yet.
Anyway, I found a workaround which you can use for now:
import { Entity, model, property, Model } from '@loopback/repository';
import { getJsonSchema } from '@loopback/repository-json-schema';
@model()
export class MyModel extends Model {
@property({
type: 'string',
required: true,
})
name: string;
constructor(data?: Partial<MyModel>) {
super(data);
}
static initialize() {
let jsonSchema = getJsonSchema(MyModel) as any;
jsonSchema.properties.name.minLength = 2;
jsonSchema.properties.name.maxLength = 255;
}
}
MyModel.initialize();
Note, all the magic happens in the MyModel.initialize
method where I initialize jsonSchema
using a standard getJsonSchema
function (part of loopback). Then I extend this jsonSchema
with additional minLength
and maxLength
properties. Inside of the getJsonSchema
function they use a cache for json schemas, so the schema is generated just once for every model during the application lifecicle which ensures the values we set will stay there every time this json schema is requested later.
You can also see related issues on the Loopback Next's Github page:
- Epic: Validation at Model/ORM level
- Complex OpenAPI Validations with
@property
Hopefully, they will support these types of validations as well as custom validators natively in Loopback decorators at some point.