Code Explainers

Code explainers tagged #validation

go
package main
 
import (
	"errors"

Parsing and validating CLI flags in Go

cli-parsing validation error-handling
Intermediate 8 steps
javascript
'use server'
 
import { revalidatePath } from 'next/cache'
import { redirect } from 'next/navigation'

How a Next.js Server Action updates a post

server-actions authorization validation
Intermediate 7 steps
php
<?php
 
namespace App\Support;
 

Merging query params onto a URL in PHP

url-parsing query-strings immutability
Intermediate 8 steps
javascript
const transitions = {
  cart: { checkout: 'shipping' },
  shipping: { submitAddress: 'payment', back: 'cart' },
  payment: { submitPayment: 'review', back: 'shipping' },

A finite state machine for checkout flow

state-machine event-driven data-driven-design
Intermediate 7 steps
php
<?php
 
namespace App\Rules;
 

How a custom phone validation rule works in Laravel

validation custom-rules dependency
Intermediate 6 steps
php
<?php
 
namespace App\Http\Controllers\Auth;
 

How a Laravel registration endpoint works

validation database-transactions events
Intermediate 8 steps
javascript
import Papa from 'papaparse';
 
const MAX_SIZE = 5 * 1024 * 1024;
 

Validating and parsing CSV uploads in the browser

promises csv-parsing validation
Intermediate 8 steps
go
package validators
 
import (
	"regexp"

Custom validators in Gin

validation sync.once regexp
Intermediate 5 steps
php
<?php
 
namespace App\Http\Controllers;
 

Handling avatar uploads in Laravel

file-upload image-processing validation
Intermediate 8 steps
ruby
class HostnameValidator < ActiveModel::EachValidator
  MAX_LENGTH = 253
  LABEL = /\A(?!-)[a-z0-9-]{1,63}(?<!-)\z/i
 

Writing a custom hostname validator in Rails

validation custom validator regex
Intermediate 8 steps
typescript
import { z } from "zod";
 
const ProductQuerySchema = z.object({
  page: z.coerce.number().int().positive().default(1),

Validating URL query params with Zod

validation schema type-inference
Intermediate 8 steps
java
public final class RegistrationValidator {
 
    private static final Pattern EMAIL = Pattern.compile("^[\\w.+-]+@[\\w-]+\\.[\\w.-]+$");
    private static final Pattern USERNAME = Pattern.compile("^[a-zA-Z0-9_]{3,20}$");

Accumulating validation errors in Java

validation regex error-accumulation
Beginner 9 steps
rust
use std::error::Error;
use std::fmt;
 
#[derive(Debug)]

Building a typed error enum in Rust

error-handling enums trait-implementation
Intermediate 9 steps
java
package com.example.payments.config;
 
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;

Type-safe config with Spring records

configuration validation records
Intermediate 6 steps
php
<?php
 
namespace App\Http\Requests;
 

How a Laravel FormRequest validates input

validation form-request input-sanitization
Intermediate 6 steps
ruby
require 'optparse'
require 'ostruct'
 
def parse_options(argv)

Parsing CLI options with OptionParser

cli argument-parsing validation
Intermediate 8 steps
typescript
import { IsEmail, IsEnum, IsInt, IsOptional, IsString, Length, Max, Min } from 'class-validator';
import { Type } from 'class-transformer';
import { Body, Controller, Post } from '@nestjs/common';
 

How DTO validation works in NestJS

validation data-transfer-objects decorators
Intermediate 8 steps
ruby
class User < ApplicationRecord
  has_one_attached :avatar
 
  validate :acceptable_avatar

Validating and serving avatars with Active Storage in Rails

active storage file uploads validation
Intermediate 9 steps