Total.js Platform
Total.js Platform

Total.js Platform news and tutorials

Building SaaS Website #09: Mastering Schemas and Validation

Building SaaS Website #09: Mastering Schemas and Validation

Welcome back to our TotalGPT development series! In this post, we'll dive deep into schemas and validation in Total.js v5. Understanding schemas is crucial for building robust applications as they help ensure data integrity and provide a structured way to handle business logic.

The Power of Schema Validation

Schema validation is a crucial aspect of web development that helps ensure data consistency and reliability. In Total.js v5, schemas serve multiple purposes:

  • Data validation
  • Business logic organization
  • API endpoint definitions
  • Database operations structuring

Let's explore how to implement schemas effectively in our TotalGPT project.


Understanding Schema Structure

In Total.js v5, schemas are defined using the NEWSCHEMA function. Here's the basic structure:

Key components:

  • SchemaName: Unique identifier for your schema
  • action: Defines operations within the schema
  • input: Validates incoming data
  • params: Validates route parameters
  • action: Contains the business logic

Creating Our First Schema: ContactForm

Let's examine our ContactForm schema implementation:

Let's break down the key elements:

  1. Input Validation:
    • *name:String: Required name field
    • *email:Email: Required and must be valid email format
    • *phone:String: Required phone number
    • *subject:String: Required subject line
    • *content:String: Required message content
  1. Automatic Validation: The asterisk (*) indicates required fields
  1. Data Processing:
    • UID(): Generates unique identifier
    • NOW: Captures current timestamp

Implementing Complex Schemas: Plan Management

Let's examine our more complex Plan schema that manages subscription plans:

Notice how we handle different operations within the same schema:

  • List operation: Retrieves all plans
  • Read operation: Gets a specific plan by ID
  • Each operation has its own validation rules and business logic

Connecting Schemas to Routes

In Total.js v5, connecting schemas to routes is straightforward. Here's how we do it in controllers/default.js:

Key points about routing:

  1. The --> syntax indicates schema routing
  2. Format: HTTP_METHOD /path/ --> SchemaName/actionName
  3. Using ACTION() to invoke schema actions programmatically

Schema Validation Types

Total.js v5 supports various validation types:



Additional validation features:

  • Custom error messages
  • Regular expression validation
  • Custom validation functions
  • Array validation

Feel free to refer to this link to have the whole list of data types and default values.


Best Practices

When working with schemas in Total.js v5:

  1. Organized Structure:

`javascript

// Group related actions together

NEWSCHEMA('EntityName', function(schema) {

// List operations first

schema.action('list', {...});

// CRUD operations next

schema.action('create', {...});

schema.action('read', {...});

schema.action('update', {...});

schema.action('remove', {...});

// Custom operations last

schema.action('custom', {...});

});

`

  1. Consistent Naming:
    • Use clear, descriptive schema names
    • Follow a consistent action naming convention
    • Document complex validation rules
  1. Error Handling:

`javascript

schema.action('create', {

action: function($, model) {

try {

// Your logic here

} catch (err) {

$.invalid(err); // Proper error handling

}

}

});

`


What's Next?

In the next blog post, we'll explore how to integrate payment gateways into our TotalGPT platform, building on the schema knowledge we've gained here.

Remember that proper schema implementation is crucial for maintaining data integrity and creating a robust application. Take time to plan your schemas carefully, considering both current and future needs of your application.

Stay tuned for more Total.js development insights in our next post! 🚀