Schema
A schema defines how the data will be sent over the network which similar to DTO in NestJS. You could use the createSchemaPolicy function to create a strapi policy
import { createSchemaPolicy } from '@/schema';
import { RegistrationSchema } from '@/schema/auth/Registration';
module.exports = createSchemaPolicy(RegistrationSchema, 'body');
Joi
Joi is used for the schema definition. Be careful there is some preset configuration
Joi.string()will automatically trimed- The
presenceoption isrequiredby default. This means all fields defined in the schema are mark asrequired()unless you addoptional(). Also fields that not defined in the schema will be consider as invalid
For more details, see strapi/schema/joi/joi.ts
Custom schema
Joi.mongoId()- Valid if the value is a mongoidJoi.file()- Valid if the value isFileinstance offormidable. Also you could validate the file size and extension with.maxSize(1)andextension(['png'])Joi.enum({ EnumObject })- Equals toJoi.valid(...Object.values(EnumObject))
Notes
- Be careful when using the
Joi.default(), If it will share with others. For exampleconst create = {
subscribed: Joi.boolean().default(false)
};
const update = {
subscribed: create.subscribed.optional()
};
const result = update.validate({});
console.log(result.value); // { subscribed: false }
Create a schema
See Helper Scripts