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,4 +1,29 @@
|
||||
import Model from './Model';
|
||||
import Model, { ModelValidator } from './Model';
|
||||
import * as yup from 'yup';
|
||||
import ResponseHandler from './ResponseHandler';
|
||||
|
||||
export const SearchType = ['song', 'artist', 'album'] as const;
|
||||
export type SearchType = (typeof SearchType)[number];
|
||||
|
||||
const SearchHistoryValidator = yup
|
||||
.object({
|
||||
query: yup.string().required(),
|
||||
type: yup.mixed<SearchType>().oneOf(SearchType).required(),
|
||||
userId: yup.number().required(),
|
||||
searchDate: yup.date().required(),
|
||||
})
|
||||
.concat(ModelValidator);
|
||||
|
||||
export const SearchHistoryHandler: ResponseHandler<
|
||||
yup.InferType<typeof SearchHistoryValidator>,
|
||||
SearchHistory
|
||||
> = {
|
||||
validator: SearchHistoryValidator,
|
||||
transformer: (value) => ({
|
||||
...value,
|
||||
timestamp: value.searchDate,
|
||||
}),
|
||||
};
|
||||
|
||||
interface SearchHistory extends Model {
|
||||
query: string;
|
||||
|
||||
Reference in New Issue
Block a user