|
| 1 | +# ActiveModel S3 Bucket Name Validator |
| 2 | + |
| 3 | +ActiveModel validator for Amazon S3 bucket names. It implements the official AWS naming rules for: |
| 4 | +- General purpose buckets (classic S3) |
| 5 | +- Directory buckets (S3 Express One Zone) |
| 6 | +- S3 Tables buckets |
| 7 | + |
| 8 | +Works in any class using ActiveModel::Validations and in Rails/ActiveRecord models. Ships with i18n messages in multiple locales. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +Add to your Gemfile: |
| 13 | + |
| 14 | +```ruby |
| 15 | +gem "activerecord-s3-bucket-name-validator" |
| 16 | +``` |
| 17 | + |
| 18 | +Then bundle: |
| 19 | + |
| 20 | +```bash |
| 21 | +bundle install |
| 22 | +``` |
| 23 | + |
| 24 | +## Compatibility |
| 25 | + |
| 26 | +- Ruby: 3.1, 3.2, 3.3, 3.4 (CI runs 3.2–3.4) |
| 27 | +- ActiveModel: 7.2, 8.0 (CI targets) |
| 28 | +- Rails/ActiveRecord: supported via ActiveModel::Validations (no Rails runtime dependency) |
| 29 | + |
| 30 | +## Quickstart |
| 31 | + |
| 32 | +Plain ActiveModel: |
| 33 | + |
| 34 | +```ruby |
| 35 | +class StorageConfig |
| 36 | + include ActiveModel::Model |
| 37 | + attr_accessor :bucket_name |
| 38 | + validates :bucket_name, s3_bucket_name: true |
| 39 | +end |
| 40 | +``` |
| 41 | + |
| 42 | +ActiveRecord model: |
| 43 | + |
| 44 | +```ruby |
| 45 | +class Storage < ApplicationRecord |
| 46 | + validates :bucket_name, s3_bucket_name: true |
| 47 | +end |
| 48 | +``` |
| 49 | + |
| 50 | +## Options |
| 51 | + |
| 52 | +- `type` — `:general_purpose` (default), `:directory`, or `:table` |
| 53 | +- `transfer_acceleration` — when `true`, periods are forbidden for general-purpose buckets |
| 54 | + |
| 55 | +Examples: |
| 56 | + |
| 57 | +```ruby |
| 58 | +validates :bucket_name, s3_bucket_name: { transfer_acceleration: true } |
| 59 | +validates :bucket_name, s3_bucket_name: { type: :directory } |
| 60 | +validates :bucket_name, s3_bucket_name: { type: :table } |
| 61 | +``` |
| 62 | + |
| 63 | +## i18n |
| 64 | + |
| 65 | +Error keys provided: |
| 66 | +- `activemodel.errors.messages.s3_bucket_name_invalid_general` |
| 67 | +- `activemodel.errors.messages.s3_bucket_name_invalid_transfer_acceleration` |
| 68 | +- `activemodel.errors.messages.s3_bucket_name_invalid_directory` |
| 69 | +- `activemodel.errors.messages.s3_bucket_name_invalid_table` |
| 70 | + |
| 71 | +Locales shipped: en, es, it, fr, de, pt-BR, ja, ko, zh-CN, zh-TW, ru, nl. |
| 72 | + |
| 73 | +Rails loads locales via a Railtie; plain ActiveModel loads them at require-time. Override by adding your own YAML with the same keys. |
| 74 | + |
| 75 | +## What’s validated (high level) |
| 76 | + |
| 77 | +- Length 3–63, allowed characters, begin/end alphanumeric |
| 78 | +- No adjacent periods; not IP-like (general purpose) |
| 79 | +- Reserved prefixes/suffixes per AWS docs (for example `xn--`, `sthree-`, `amzn-s3-demo-`, `-s3alias`, `--ol-s3`, `.mrap`, `--x-s3`, `--table-s3`) |
| 80 | +- Directory buckets must end with `--<zone-id>--x-s3` |
| 81 | +- S3 Tables buckets disallow periods and underscores |
| 82 | +- Optional TA mode forbids periods |
| 83 | + |
| 84 | +Note: Global/partition uniqueness and immutability are service-side constraints and not enforced locally. |
| 85 | + |
| 86 | +## Examples |
| 87 | + |
| 88 | +Valid (general purpose): |
| 89 | + |
| 90 | +```text |
| 91 | +my-example-bucket |
| 92 | +example.com |
| 93 | +``` |
| 94 | + |
| 95 | +Invalid (general purpose): |
| 96 | + |
| 97 | +```text |
| 98 | +ex..ample |
| 99 | +192.168.5.4 |
| 100 | +xn--abc |
| 101 | +foo--x-s3 |
| 102 | +``` |
| 103 | + |
| 104 | +## Running tests (for contributors) |
| 105 | + |
| 106 | +```bash |
| 107 | +# ActiveModel-only |
| 108 | +bundle install |
| 109 | +bundle exec rake test |
| 110 | + |
| 111 | +# With ActiveRecord integration (in-memory sqlite) |
| 112 | +AR_INTEGRATION=1 ACTIVERECORD_VERSION="~> 8.0" bundle install |
| 113 | +AR_INTEGRATION=1 ACTIVERECORD_VERSION="~> 8.0" bundle exec rake test |
| 114 | +``` |
| 115 | + |
| 116 | +## License |
| 117 | + |
| 118 | +MIT — see `LICENSE.txt`. |
0 commit comments