ruby
class Order < ApplicationRecord
  has_many :line_items, inverse_of: :order, dependent: :destroy

  accepts_nested_attributes_for :line_items,
    allow_destroy: true,
    reject_if: ->(attrs) { attrs["product_id"].blank? && attrs["quantity"].blank? }

Validate Nested Attributes for an Order and Its Line Items in Rails

rails activerecord validations
by codesnips 3 tabs
typescript
import { z } from "zod";

export const signupSchema = z
  .object({
    email: z.string().min(1, "Email is required").email("Enter a valid email"),
    username: z

React Signup Form Validation With Zod and Field-Level Errors

react zod forms
by codesnips 3 tabs
go
package export

import (
	"encoding/csv"
	"log"
	"net/http"

Streaming Large CSV Exports in Go Without Buffering the Whole File

go http csv
by codesnips 3 tabs
rust
use chrono::NaiveDate;
use rust_decimal::Decimal;
use serde::{Deserialize, Deserializer};

#[derive(Debug, Clone, Deserialize)]
pub struct Transaction {

Aggregate CSV Transaction Totals per Category With Serde and csv Crate

rust csv serde
by codesnips 3 tabs
rust
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Interval {
    pub start: i64,
    pub end: i64,
}

Merging Overlapping Booking Intervals with a Sweep-Line in Rust

rust algorithms intervals
by codesnips 3 tabs
rust
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;
use uuid::Uuid;

#[derive(Debug, Clone, Serialize, Deserialize)]

Cursor-Paginated Streaming REST Endpoint in Axum with Keyset Pagination

axum rust pagination
by codesnips 3 tabs
rust
use serde::{Deserialize, Deserializer};
use time::OffsetDateTime;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UserProfile {

Deserializing Optional and Renamed JSON API Fields with Serde in Rust

rust serde serde-json
by codesnips 2 tabs
javascript
export const initialState = (steps, initialData = {}) => ({
  steps,
  stepIndex: 0,
  data: initialData,
  errors: {},
});

Multi-Step Form Wizard in React With a useReducer State Machine

react hooks usereducer
by codesnips 4 tabs
javascript
class TokenBucket {
  constructor(capacity, refillRatePerSec) {
    this.capacity = capacity;
    this.refillRate = refillRatePerSec;
    this.tokens = capacity;
    this.lastRefill = Date.now();

Token Bucket Rate Limiter as Express Middleware

express rate-limiting token-bucket
by codesnips 3 tabs
php
<?php

namespace App\Models;

use App\Notifications\VerifyEmailNotification;
use Illuminate\Contracts\Auth\MustVerifyEmail;

Signed Email-Verification Links with Custom Laravel Notification and Signed Routes

laravel signed-urls email-verification
by codesnips 4 tabs
python
from django.db import models
from django.utils.text import slugify


class Article(models.Model):
    title = models.CharField(max_length=200)

Auto-Generate Unique Slugs in Django with a pre_save Signal and Model Method

django signals slugs
by codesnips 3 tabs
ruby
class DownloadsController < ApplicationController
  before_action :authenticate_user!

  def show
    report = current_account.reports.find(params[:report_id])
    authorize! :download, report

Signed, Expiring S3 Download URLs for Active Storage Attachments in Rails

rails active-storage s3
by codesnips 4 tabs