You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Make NCES ID mandatory for US schools
Add conditional presence validation for US NCES ID (district_nces_id)
fields, with format validation and scoped uniqueness to allow reuse
after school rejection. This improves validation error handling so users
receive specific, actionable error messages instead of generic form
errors.
## Status
Closes:
https://github.com/orgs/RaspberryPiFoundation/projects/51/views/11?pane=issue&itemId=120513360&issue=RaspberryPiFoundation%7Cdigital-editor-issues%7C780
Related to: (standalone url)
## Points for consideration:
### Security
- Partial unique indexes prevent duplicate entries among active
(non-rejected) schools
- Format validation enforces strict digit patterns (5-6 digits for
[URN](https://get-information-schools.service.gov.uk/glossary), 12
digits for [NCES ID](https://nces.ed.gov/ccd/psadd.asp))
- Case-insensitive uniqueness check prevents bypass attempts
## What's changed?
### Database:
- Converted `reference` index to partial unique index (`WHERE
rejected_at IS NULL`)
- Converted `district_nces_id` index to partial unique index (`WHERE
rejected_at IS NULL`)
- This allows rejected schools to release their identifiers for reuse by
legitimate schools
### Model (School):
- Added conditional presence validation: `reference` required for UK
(`country_code == 'GB'`)
- Added conditional presence validation: `district_nces_id` required for
US (`country_code == 'US'`)
- Added format validation: URN must be 5-6 digits, NCES ID must be 12
digits
- Added scoped uniqueness: only active (non-rejected) schools are
checked for duplicates
- Added `united_kingdom?` and `united_states?` helper methods
### API Error Responses:
| Field | Validation | Error Message |
|-------|------------|---------------|
| `reference` | presence | `"can't be blank"` |
| `reference` | uniqueness | `"has already been taken"` |
| `reference` | format | `"must be 5-6 digits (e.g., 100000)"` |
| `district_nces_id` | presence | `"can't be blank"` |
| `district_nces_id` | uniqueness | `"has already been taken"` |
| `district_nces_id` | format | `"must be 12 digits (e.g.,
010000000001)"` |
### Tests:
- 16 comprehensive validation tests covering:
- Conditional presence (UK/US specific)
- Format validation (valid/invalid patterns)
- Scoped uniqueness (allows reuse after rejection)
- Updated factory to include valid `reference` for default GB schools
---------
Co-authored-by: Jamie Benstead <57325966+jamiebenstead@users.noreply.github.com>
Co-authored-by: Jamie Benstead <jamie.benstead@gmail.com>
0 commit comments