Code Explainers

Code explainers tagged #validation

go
package config
 
import (
	"fmt"

Parsing timeout config in Go

configuration validation error-wrapping
Intermediate 7 steps
go
func (h *WebhookHandler) HandleBatch(c *gin.Context) {
	var payload BatchWebhookPayload
	if err := c.ShouldBindJSON(&payload); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "invalid payload"})

Handling batched webhooks in Gin

webhooks signature-verification job-queue
Intermediate 7 steps
php
<?php
 
namespace App\Http\Controllers\Api;
 

Building a cursor-paginated feed in Laravel

cursor-pagination eager-loading validation
Intermediate 8 steps
java
@Controller
@RequestMapping("/employees")
public class EmployeeController {
 

Customizing form binding in a Spring MVC controller

data-binding validation type-conversion
Intermediate 9 steps
ruby
require "phonelib"
 
class PhoneNumber
  class InvalidNumber < StandardError; end

Wrapping phone parsing in a Ruby value object

value-object memoization validation
Intermediate 7 steps
typescript
import { Component } from '@angular/core';
import { NgForm } from '@angular/forms';
 
interface SignupModel {

How template-driven forms validate in Angular

forms two-way-binding validation
Intermediate 9 steps
python
from flask import Blueprint, request, jsonify
from marshmallow import Schema, fields, validate, ValidationError, EXCLUDE
 
from .models import db, User

How a Flask blueprint validates and creates users

validation rest-api schema
Intermediate 8 steps
python
import uuid
from pathlib import Path
 
from fastapi import APIRouter, File, Form, HTTPException, UploadFile

Handling multipart file uploads in FastAPI

file-upload validation multipart-form
Intermediate 6 steps
go
package handlers
 
import (
	"net/http"

Custom query validation in Gin

validation query-binding deduplication
Intermediate 8 steps
javascript
import { NextResponse } from 'next/server';
import { createWriteStream } from 'node:fs';
import { mkdir } from 'node:fs/promises';
import { pipeline } from 'node:stream/promises';

Streaming file uploads in a Next.js route

file-upload streams validation
Intermediate 9 steps
go
func handleUpload(w http.ResponseWriter, r *http.Request) {
	if err := r.ParseMultipartForm(32 << 20); err != nil {
		http.Error(w, "failed to parse form: "+err.Error(), http.StatusBadRequest)
		return

Handling multi-file uploads in Go

multipart file-upload http-handler
Intermediate 9 steps
typescript
import {
  Controller,
  Get,
  Query,

Validating paginated queries in NestJS

pagination validation pipes
Intermediate 7 steps
typescript
import {
  Controller,
  Post,
  UploadedFile,

Handling avatar uploads in NestJS

file-upload validation interceptors
Intermediate 7 steps
java
public record ServerConfig(
        String host,
        int port,
        Duration timeout,

Loading typed config into a Java record

records factory-methods parsing
Intermediate 7 steps
java
public final class IbanValidator {
 
    private static final Map<String, Integer> COUNTRY_LENGTHS = Map.ofEntries(
        Map.entry("DE", 22), Map.entry("FR", 27), Map.entry("GB", 22),

Validating IBANs with the mod-97 checksum

validation checksum modular-arithmetic
Intermediate 8 steps
go
package config
 
import (
	"fmt"

Loading and saving YAML config in Go

yaml struct-tags defaults
Intermediate 8 steps
php
<?php
 
namespace App\Http\Controllers;
 

Streaming a filtered CSV export in Laravel

streaming csv-export query-builder
Intermediate 9 steps
go
package handler
 
type LineItem struct {
	SKU      string  `json:"sku" binding:"required,alphanum"`

Structured validation errors in Gin

validation json-binding error-handling
Intermediate 8 steps