typescript
import { z } from "zod";

export const rowSchema = z.object({
  email: z.string().email(),
  name: z.string().min(1, "name is required"),
  age: z.coerce.number().int().min(0, "age must be >= 0"),

Parse a CSV Upload into Typed Rows with Per-Row Validation Errors

typescript csv validation
by codesnips 3 tabs
ruby
class InvoicesController < ApplicationController
  def index
    invoices = InvoicesQuery.new(current_account.invoices, filter_params).call

    @invoices = invoices.page(params[:page]).per(25)
    render :index

Building a Composable Query Object for Filtering Rails ActiveRecord Scopes

rails activerecord query-object
by codesnips 3 tabs
python
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models, transaction

from .middleware import get_current_user

Auditing Django Model Field Changes in an Overridden save() Method

django audit-log orm
by codesnips 3 tabs
rust
mod chunker;mod parallel_hash;

use parallel_hash::{hash_buffer, root_hash};

const CHUNK_SIZE: usize = 64 * 1024;

Content-Defined Chunking With Parallel BLAKE3 Hashing Using Rayon

rust rayon parallelism
by codesnips 3 tabs
javascript
'use strict';

const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
const crypto = require('crypto');

Stream a Multipart Upload Through Gzip to Disk with stream.pipeline

nodejs streams backpressure
by codesnips 2 tabs
typescript
import { RefObject, useEffect, useRef } from "react";

type Handler = (event: MouseEvent | TouchEvent) => void;

export function useClickOutside<T extends HTMLElement>(
  ref: RefObject<T>,

Reusable useClickOutside Hook for Closing Dropdowns in React

react hooks typescript
by codesnips 3 tabs
javascript
function escapeCell(value) {
  if (value === null || value === undefined) return '';
  const str = String(value);
  if (/[",\n\r]/.test(str)) {
    return '"' + str.replace(/"/g, '""') + '"';
  }

Stream a Large CSV Export in Express with Backpressure and an Async Row Generator

express streaming csv
by codesnips 3 tabs
python
import base64
import json
from datetime import datetime


class InvalidCursor(Exception):

Cursor-Based Pagination for a Flask JSON API Blueprint

flask pagination cursor
by codesnips 3 tabs
ruby
class Article < ApplicationRecord
  has_many :taggings, dependent: :destroy
  has_many :tags, through: :taggings

  scope :published, -> { where.not(published_at: nil) }

Filtering a Listing by Tags with a has_many :through Scope and a Query Object

rails activerecord has-many-through
by codesnips 3 tabs
rust
use async_trait::async_trait;
use std::io;

#[async_trait]
pub trait ConnectionFactory: Send + Sync + 'static {
    type Connection: Send + 'static;

Building a Bounded Async Database Connection Pool With Tokio Semaphore

rust tokio async
by codesnips 3 tabs
typescript
import { SetMetadata } from '@nestjs/common';

export enum Role {
  User = 'user',
  Editor = 'editor',
  Admin = 'admin',

Role-Based Access Control in NestJS with a Custom Guard and @Roles Decorator

nestjs authorization rbac
by codesnips 3 tabs
go
package authtransport

import (
	"errors"
	"net/http"
)

Injecting Auth Headers via a Custom http.RoundTripper Decorator in Go

go http middleware
by codesnips 3 tabs