GorimGorim
  • Introduction
  • Getting Started
  • Configuration
  • Routing
  • Middleware
  • Serializers
  • Views
  • Mixins
  • Filter
  • Pagination
  • Permissions
  • Migrations
  • Error Handling
GitHub
  • Introduction
  • Getting Started
  • Configuration
  • Routing
  • Middleware
  • Serializers
  • Views
  • Mixins
  • Filter
  • Pagination
  • Permissions
  • Migrations
  • Error Handling
GitHub
  • Guide

    • Introduction
    • Getting Started
    • Serializers
      • Basic Usage
      • Validation
      • Single Field Validation
    • Views
    • Mixins
    • Filter
    • Pagination
    • Routing
    • Middleware
    • Permissions
    • Migrations
    • Error Handling

Serializers

Gorim provides a powerful way to convert JSON to struct with complex validation and serialization logic.

Basic Usage

import (
	"gorim.org/gorim/serializers"
	"myproject/models"
)

type UserSerializer struct {
    serializers.ModelSerializer[models.User]
    ID   int    `json:"id"`
    Name string `json:"name"`
}

Validation

Gorim provides a powerful way to validate data using serializers.

import (
	"gorim.org/gorim/serializers"
)

type UserSerializer struct {
    serializers.ModelSerializer[models.User]
    ID   int    `json:"id"`
    Name string `json:"name"`
}

func (s *UserSerializer) Validate() {
    // call the parent validate method first for default validation
    s.ModelSerializer.Validate()

    if s.Name == "Bad User" {
        s.AddError("name", "Name cannot be Bad User")
    }
}

Single Field Validation

Gorim provides a powerful way to validate a single field using Validate{FieldName} followed by the field name.

for example:

import (
	"gorim.org/gorim/serializers"
)

type UserSerializer struct {
    serializers.ModelSerializer[models.User]
    Name string `json:"name"`
}

func (s *UserSerializer) ValidateName() {
    if s.Name == "Bad User" {
        s.AddError("name", "Name cannot be Bad User")
    }
}
Edit this page
Last Updated:
Contributors: Rimba Prayoga
Prev
Getting Started
Next
Views