typescript
export type Validator = (value: string) => string | null;

export const required = (message = 'This field is required'): Validator => {
  return (value) => (value.trim().length === 0 ? message : null);
};

Field-Level Form Validation with a Reusable useField Hook in React

react hooks forms
by codesnips 3 tabs
java
package com.example.security;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

Resolve the Authenticated User into a Controller Argument in Spring Boot

spring-boot spring-mvc security
by codesnips 4 tabs
rust
use std::collections::HashMap;
use std::hash::Hash;

const NIL: usize = usize::MAX;

struct Node<K, V> {

Build an O(1) LRU Cache in Rust With a HashMap and Intrusive Doubly Linked List

rust lru cache
by codesnips 3 tabs
php
<?php

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

Validate an Unredeemed Coupon Code with a Custom Laravel Rule Object

laravel validation eloquent
by codesnips 4 tabs
ruby
class CreateStripeEvents < ActiveRecord::Migration[7.1]
  def change
    create_table :stripe_events do |t|
      t.string :stripe_event_id, null: false
      t.string :event_type, null: false
      t.string :status, null: false, default: "received"

Idempotent Stripe Webhook Processing in Rails with a Durable Event Log

rails stripe webhooks
by codesnips 4 tabs
python
from typing import Literal, Tuple, Type

from pydantic import BaseModel, PostgresDsn, RedisDsn
from pydantic_settings import (
    BaseSettings,
    PydanticBaseSettingsSource,

Layered Pydantic Settings: Env Vars Over a YAML Defaults File

pydantic configuration settings
by codesnips 4 tabs
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

Building Filterable, Sortable Product Listings with Laravel Query Scopes

laravel eloquent query-scopes
by codesnips 3 tabs
typescript
import { useEffect, useState } from "react";

export function useDebouncedValue<T>(value: T, delay = 800): T {
  const [debounced, setDebounced] = useState<T>(value);

  useEffect(() => {

Debounced Draft Autosave with React Query and a Saving-Status Indicator

react react-query autosave
by codesnips 4 tabs
java
package com.example.cache;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CompletionException;
import java.util.concurrent.atomic.AtomicBoolean;

Single-Flight Cache Loader: Coalesce Concurrent Identical Loads in Java

caching concurrency single-flight
by codesnips 3 tabs
python
from typing import Any, Optional

from fastapi.responses import JSONResponse
from pydantic import BaseModel

Consistent JSON Error Responses in FastAPI With a Custom Exception Handler

fastapi error-handling exceptions
by codesnips 3 tabs
typescript
export type EventMap = Record<string, unknown[]>;

type Listener<Args extends unknown[]> = (...args: Args) => void;

export class TypedEmitter<Events extends EventMap> {
  private listeners = new Map<keyof Events, Set<Listener<any>>>();

Type-Safe Event Emitter With Strongly-Typed Listener Payloads in TypeScript

typescript events event-emitter
by codesnips 3 tabs
php
<?php

namespace App\Domain;

use InvalidArgumentException;

Serializing a Money Value Object with a Custom Symfony Normalizer

symfony serializer value-object
by codesnips 3 tabs