String
Creation
class User {
public name: string;
}
const USER_META = new ObjectMeta({
builder: User,
fields: {
name: new StringField(),
}
});
const values = [
{
name: `Vasya`
},
{
name: 12
},
];
const result = JTC.convert({
id: `String`,
meta: new ObjectArrayMeta({ meta: USER_META }),
values,
});
const log = JTC.log.asString(result.tree);
console.log(result.converted.all);
/* 1 (EXCLUDED) -> {...} | Validation failed for all fields
1 (EXCLUDED) -> name -> 12 | Expected string, but got number */
console.log(log);
Validation
Enum
Allows to check that value is a member of specific enum
enum EUserType {
admin = `admin`,
user = `user`,
manager = `manager`
}
class User {
public type: EUserType;
}
const typeField = new StringField({
enum: EUserType
});
const USER_META = new ObjectMeta({
builder: User,
fields: {
type: typeField
}
});
const values = [
{
type: EUserType.admin
},
{
type: EUserType.user
},
{
type: `anonymous`
},
{
type: EUserType.manager
},
];
const result = JTC.convert({
id: `Enum`,
meta: new ObjectArrayMeta({ meta: USER_META }),
values,
});
const validation = typeField.validate({ value: `guest` });
const log = JTC.log.asString(result.tree);
console.log(result.converted.valid);
/* 2 -> type -> anonymous | Not present in enum: admin, user, manager */
console.log(log);
/* [ { value: 'guest', code: 4, message: 'Not present in enum: admin, user, manager' } ] */
console.log(validation.errors);
/* true */
console.log(validation.isInvalid);
Min / Max
Allows to check that string have a specific length
class User {
public name: string;
}
const nameField = new StringField({
minLength: 4,
maxLength: 10,
});
const USER_META = new ObjectMeta({
builder: User,
fields: {
name: nameField
}
});
const values = [
{
name: `Andrey`
},
{
name: `Joi`,
},
{
name: `Apollinaria`
},
];
const result = JTC.convert({
id: `Min/max`,
meta: new ObjectArrayMeta({ meta: USER_META }),
values,
});
const validation = nameField.validate({ value: `Max` });
const log = JTC.log.asString(result.tree);
console.log(result.converted.valid);
/* 1 -> name -> Joi | Length(3) is less than expected(4)
2 -> name -> Apollinaria | Length(11) is more than expected(10) */
console.log(log);
/* [ { code: 5, value: 'Max', message: 'Length(3) is less than expected(4)' } ] */
console.log(validation.errors);
/* true */
console.log(validation.isInvalid);
Pattern
Allows to check that string matches specific pattern
class User {
public email: string;
}
const emailField = new StringField({
pattern: /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/
});
const USER_META = new ObjectMeta({
builder: User,
fields: {
email: emailField
}
});
const values = [
{
email: `test@gmail.com`
},
{
email: `test.mail.com`,
},
{
email: `test@mail`
},
];
const result = JTC.convert({
id: `Pattern`,
meta: new ObjectArrayMeta({ meta: USER_META }),
values,
});
const validation = emailField.validate({ value: `test@mail@for.cu` });
const log = JTC.log.asString(result.tree);
console.log(result.converted.valid);
/* 1 -> email -> test.mail.com | Doesn't match pattern */
console.log(log);
/* [ { code: 9, message: 'Doesn\'t match pattern', value: 'test@mail@for.cu' } ] */
console.log(validation.errors);
/* true */
console.log(validation.isInvalid);
Last updated
Was this helpful?