python
import re

EMAIL_RE = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")


class SignupValidator:

Validate a Flask Signup Form and Collect Per-Field Errors

flask validation forms
by codesnips 4 tabs
javascript
export function clamp(value, min, max) {
  return Math.min(Math.max(value, min), max);
}

export function move(list, from, to) {
  const next = list.slice();

Drag-and-Drop Sortable List with Pointer Events and an Immutable Reorder Helper

react drag-and-drop pointer-events
by codesnips 3 tabs
ruby
class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user, record)
    @user = user
    @record = record

Authorizing Controller Actions with Pundit Policy Objects in Rails

rails pundit authorization
by codesnips 3 tabs
php
<?php

namespace App\Tenancy;

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

Enforce Multi-Tenant Isolation in Laravel with a Global Scope Bound to the Current User

laravel multi-tenancy eloquent
by codesnips 4 tabs
rust
use std::path::{Component, Path, PathBuf};

#[derive(Debug)]
pub enum ResolveError {
    Traversal,
    NotFound,

Safely Serving Static Files with Path Traversal Protection in Rust

rust axum tokio
by codesnips 3 tabs
typescript
import {
  WebSocketGateway,
  WebSocketServer,
  SubscribeMessage,
  OnGatewayConnection,
  OnGatewayDisconnect,

Room-Based Live Notifications over a NestJS WebSocket Gateway

nestjs websockets socket-io
by codesnips 4 tabs
python
from werkzeug.routing import BaseConverter
from werkzeug.exceptions import NotFound
from flask import abort


class ModelConverter(BaseConverter):

Auto-Loading SQLAlchemy Models in Flask Routes With a Custom URL Converter

flask werkzeug sqlalchemy
by codesnips 3 tabs
javascript
import { useReducer, useCallback } from 'react';

const initialState = (present) => ({ past: [], present, future: [] });

function historyReducer(state, action) {
  const { past, present, future } = state;

Build an Undo/Redo History Stack with a useHistory Reducer Hook in React

react hooks usereducer
by codesnips 3 tabs
python
from enum import Enum
from typing import Optional

from fastapi import Query
from pydantic import BaseModel, Field, field_validator

Validate and Normalize Query Filters With a Pydantic Dependency in FastAPI

fastapi pydantic validation
by codesnips 3 tabs
ruby
class CounterFlushJob
  include Sidekiq::Job
  sidekiq_options queue: :counters, retry: 3

  def perform
    CounterBuffer.flush_all.each do |field, delta|

Coalesced Counter Cache with Redis Buffering and Nightly Reconciliation in Rails

rails counter-cache redis
by codesnips 4 tabs
go
package httpstream

import (
	"bufio"
	"database/sql"
	"fmt"

Streaming Large SQL Query Results as a JSON Array in Go

go database sql
by codesnips 2 tabs
typescript
import { Exclude, Expose } from 'class-transformer';

export class User {
  id: string;

  firstName: string;

Hide Sensitive Fields in NestJS Responses with class-transformer and ClassSerializerInterceptor

nestjs class-transformer serialization
by codesnips 3 tabs