Front: Api models validation (#245)
* Front: Model: Write Validators * Front: Plage response validator * Front: API: Typing 'fetch' return * Front: Basic Models: Response Handlers * Front: API: Validate authentication response * Front: Validate Search History * Front: Validate Responses of User updates * Front: On Validation Error, more verbose console error
This commit is contained in:
@@ -1,8 +1,44 @@
|
||||
interface SongHistory {
|
||||
import * as yup from 'yup';
|
||||
import ResponseHandler from './ResponseHandler';
|
||||
|
||||
export const SongHistoryItemValidator = yup.object({
|
||||
songID: yup.number().required(),
|
||||
userID: yup.number().required(),
|
||||
score: yup.number().required(),
|
||||
difficulties: yup.mixed().required(),
|
||||
});
|
||||
|
||||
export const SongHistoryItemHandler: ResponseHandler<
|
||||
yup.InferType<typeof SongHistoryItemValidator>,
|
||||
SongHistoryItem
|
||||
> = {
|
||||
validator: SongHistoryItemValidator,
|
||||
transformer: (value) => ({
|
||||
...value,
|
||||
difficulties: value.difficulties,
|
||||
}),
|
||||
};
|
||||
|
||||
export const SongHistoryValidator = yup.object({
|
||||
best: yup.number().required().nullable(),
|
||||
history: yup.array(SongHistoryItemValidator).required(),
|
||||
});
|
||||
|
||||
export type SongHistory = yup.InferType<typeof SongHistoryValidator>;
|
||||
|
||||
export const SongHistoryHandler: ResponseHandler<SongHistory> = {
|
||||
validator: SongHistoryValidator,
|
||||
transformer: (value) => ({
|
||||
...value,
|
||||
history: value.history.map((item) => SongHistoryItemHandler.transformer(item)),
|
||||
}),
|
||||
};
|
||||
|
||||
export type SongHistoryItem = {
|
||||
songID: number;
|
||||
userID: number;
|
||||
score: number;
|
||||
difficulties: JSON;
|
||||
}
|
||||
difficulties: object;
|
||||
};
|
||||
|
||||
export default SongHistory;
|
||||
|
||||
Reference in New Issue
Block a user