arktypea
arktype•2y ago
Dimava

Usage for HTTP validation

I'm looking to use Arktype as a Request/Response validator
This comes with the following requirements:
  • Request body should be validated with deep reject unknownKeys so invalid requests are immediately 422
  • Response should be validated with deep delete unknownKeys so local information can't passthrough
  • Response input should be cloned rather then inline-morphed (including key deletion) so you can pass in input objects as-is
let User = type({ name: string, meta: { bio: 'string' } })
let CreateUser = type([ User, '&', { password: string, meta: { age: 'number' } } ])
let UserModel = type([ CreateUser, '&', { meta: { isAdult: 'boolean' } } ])

let oneUser: UserModel;
function addUser(user: CreateUser) {
   // should throw on inputs with extra keys on user and on user.meta
   let newUser = AT_asHttpIn(CreateUser).assert(user)
   newUser.meta.isAdult = newUser.meta.age >= 18 
   oneUser = AT_strict(UserModel).assert(newUser)
}
function getUser() {
   // should remove password and age
   // should not remove them on actual data
   return AT_asHttpOut(User).assert(oneUser)
}


Please list recommended AT APIs for this
Please link the related undone issues is any
Was this page helpful?