Bounties API

Bounties API allows applications (including the Bounty Bot, Bankless Mobile App, and Bounty Board) to perform Create, Retrieve, Update and Delete operations on bounties.
Progress
Technical Spec
Validations
Schema validations are defined in two places:
Yup for API Validation
Yup is an excellent library for handling schema validation across both DOM and NodeJS environments (read: Frontend and Backend).
Here is an excellent video of getting started with NextJS and Yup:
We use yup for any validation of objects, before attempting to load to the DB. The advantage here is that any runtime transformations can be independent of the DB Schema.
Yup schema, while a little verbose, can be directly converted to typescript interfaces, so the net gain in code maintenance is worthwhile.
 
The yup validator is implemented as a middleware in the Next API route. The middleware itself takes 2 arguments:
const validate = ({ schema, handler }: ValidatorProps): ValidatorFunction
Where the schema is the yup schema, and the handler is the next API route handler.
To add the validation to your existing routes, just simply change the export in the route file from:
// src/pages/api/index/bounties/index.ts

const handler = (req, res) => {
	// ...
}
export default handler;
to:
import { schema } from 'models/[Model].ts';
import { validate } from 'middlewares/validate.ts';

const handler = (req, res) => {
	// ...
}
export default validate({ schema, handler });
 
The format of the response in the case of a validation error will be a 400 error with the following payload:
{
    "message": "2 errors occurred",
    "errors": [
        "customer_id is a required field",
        "status must be a `string` type, but the final value was: `null`.\n If \"null\" is intended as an empty value be sure to mark the schema as `.nullable()`"
    ]
}
You can freely pass around yup schema across both the front and back end, along with Typescript interfaces.
As a general rule:
  • Use a yup schema if you need to perform complex runtime validations of objects.
  • Use a typescript type or interface if you need a purely compile-time check
  • Use a javascript typeguard (instanceOf, typeof ) if you need a simple runtime type check
 
Mongoose for DRM Validation
Mongoose provides final level validation at the final stage of deserialization into the database. Importantly for NextJS, Mongoose objects cannot be passed to the frontend.
As a general rule, use a Mongoose schema sparingly, and on the server side. It's a good structural check, but shouldn't be relied upon too heavily.
Content Gateway Integration
💡
See
Content Gateway
for a full technical breakdown of the bankless content gateway project
 

Bounties API

Bounties API allows applications (including the Bounty Bot, Bankless Mobile App, and Bounty Board) to perform Create, Retrieve, Update and Delete operations on bounties.
Progress
Technical Spec
Validations
Schema validations are defined in two places:
Yup for API Validation
Yup is an excellent library for handling schema validation across both DOM and NodeJS environments (read: Frontend and Backend).
Here is an excellent video of getting started with NextJS and Yup:
We use yup for any validation of objects, before attempting to load to the DB. The advantage here is that any runtime transformations can be independent of the DB Schema.
Yup schema, while a little verbose, can be directly converted to typescript interfaces, so the net gain in code maintenance is worthwhile.
 
The yup validator is implemented as a middleware in the Next API route. The middleware itself takes 2 arguments:
const validate = ({ schema, handler }: ValidatorProps): ValidatorFunction
Where the schema is the yup schema, and the handler is the next API route handler.
To add the validation to your existing routes, just simply change the export in the route file from:
// src/pages/api/index/bounties/index.ts

const handler = (req, res) => {
	// ...
}
export default handler;
to:
import { schema } from 'models/[Model].ts';
import { validate } from 'middlewares/validate.ts';

const handler = (req, res) => {
	// ...
}
export default validate({ schema, handler });
 
The format of the response in the case of a validation error will be a 400 error with the following payload:
{
    "message": "2 errors occurred",
    "errors": [
        "customer_id is a required field",
        "status must be a `string` type, but the final value was: `null`.\n If \"null\" is intended as an empty value be sure to mark the schema as `.nullable()`"
    ]
}
You can freely pass around yup schema across both the front and back end, along with Typescript interfaces.
As a general rule:
  • Use a yup schema if you need to perform complex runtime validations of objects.
  • Use a typescript type or interface if you need a purely compile-time check
  • Use a javascript typeguard (instanceOf, typeof ) if you need a simple runtime type check
 
Mongoose for DRM Validation
Mongoose provides final level validation at the final stage of deserialization into the database. Importantly for NextJS, Mongoose objects cannot be passed to the frontend.
As a general rule, use a Mongoose schema sparingly, and on the server side. It's a good structural check, but shouldn't be relied upon too heavily.
Content Gateway Integration
💡
See
Content Gateway
for a full technical breakdown of the bankless content gateway project