ruby
class Coupon < ApplicationRecord
  before_validation :normalize_code

  validates :code,
            coupon_code: { min_length: 6 },
            uniqueness: { case_sensitive: false }

Validating Coupon Codes with a Custom EachValidator in Rails

rails validations activerecord
by codesnips 3 tabs
java
@RestController
@RequestMapping("/api/products")
public class ProductController {

    private final ProductRepository repository;
    private final JsonMergePatchService patchService;

Spring Boot PATCH Endpoint With JSON Merge Patch (RFC 7386)

spring-boot rest-api json-merge-patch
by codesnips 4 tabs
javascript
const SHUTDOWN_TIMEOUT_MS = 10_000;

function registerFatalHandlers({ logger, onFatal, exitCode = 1 }) {
  let shuttingDown = false;

  async function handleFatal(kind, error) {

Graceful Node.js Shutdown on uncaughtException and unhandledRejection

node reliability graceful-shutdown
by codesnips 3 tabs
php
<?php

namespace App\Services\RateLimit;

use Illuminate\Support\Facades\Redis;

Rate-Limited HTTP Client Wrapper With Redis Token Bucket and a Queued Dispatcher

laravel redis rate-limiting
by codesnips 4 tabs
python
from typing import List, Optional

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from .dataloader import DataLoader

Batched DataLoader to Avoid N+1 Queries in FastAPI Responses

fastapi dataloader n-plus-one
by codesnips 3 tabs
go
package mailer

import (
	"crypto/sha256"
	"encoding/hex"
	"strings"

Idempotent Email Sending with a Redis Fingerprint Set in Go

redis idempotency email
by codesnips 3 tabs
typescript
import { Knex } from 'knex';

export interface RawDailyRow {
  day: string;
  order_count: string;
  gross_cents: string;

Aggregate Daily Order Totals Into a Report With Knex and a Formatter

knex postgres reporting
by codesnips 3 tabs
erb
<h1>New Article</h1>

<form action="/articles" method="post">
  <div>
    <label for="title">Title</label>
    <input id="title" type="text" name="article[title]"

Post-Redirect-Get in Sinatra with Sinatra::Flash and a redirect_to Helper

sinatra rack flash
by codesnips 4 tabs
java
package com.example.security;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import org.springframework.beans.factory.annotation.Value;

Stateless JWT Authentication with a Token Service and a Spring Security Filter

spring-security jwt authentication
by codesnips 3 tabs
javascript
const sharp = require('sharp');

const DEFAULTS = {
  maxWidth: 1600,
  maxHeight: 1600,
  format: 'webp',

Resize and Compress Uploaded Images with Sharp Before Saving to Disk

nodejs express sharp
by codesnips 3 tabs
php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Polymorphic Media Attachments in Laravel With a Queued Image Resize Job

laravel polymorphism eloquent
by codesnips 4 tabs
rust
use std::collections::HashMap;
use std::path::PathBuf;

use walkdir::WalkDir;

pub fn group_by_size(root: &str) -> HashMap<u64, Vec<PathBuf>> {

Detect Duplicate Files with Parallel Blake3 Checksums in Rust

rust checksums blake3
by codesnips 4 tabs