Skip to content

Commit

Permalink
Better server side validation
Browse files Browse the repository at this point in the history
djhi committed Jun 6, 2024
1 parent 6d39911 commit e2f4c19
Showing 4 changed files with 81 additions and 22 deletions.
12 changes: 10 additions & 2 deletions example/App.tsx
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@ import {
ListGuesser,
Resource,
ShowGuesser,
required,
AutocompleteInput,
} from 'react-admin';
import { QueryClient } from 'react-query';

@@ -51,8 +53,14 @@ import authProvider from './authProvider';
export const BookCreate = () => (
<Create>
<SimpleForm>
<ReferenceInput source="author_id" reference="authors" />
<TextInput source="title" />
<ReferenceInput source="author_id" reference="authors">
<AutocompleteInput validate={required()} />
</ReferenceInput>
<TextInput
source="title"
validate={required()}
defaultValue="Anna Karenina"
/>
</SimpleForm>
</Create>
);
33 changes: 23 additions & 10 deletions example/fetchMock.ts
Original file line number Diff line number Diff line change
@@ -19,16 +19,29 @@ export const initializeFetchMock = () => {
if (!request.headers?.get('Authorization')) {
return new Response(null, { status: 401 });
}

if (
context.collection === 'books' &&
request.method === 'POST' &&
!context.requestJson?.title
) {
return new Response(null, {
status: 400,
statusText: 'Title is required',
});
return next(request, context);
});
restServer.addMiddleware(async (request, context, next) => {
if (context.collection === 'books' && request.method === 'POST') {
if (
restServer.collections[context.collection].getCount({
filter: {
title: context.requestJson?.title,
},
}) > 0
) {
throw new Response(
JSON.stringify({
errors: {
title: 'An article with this title already exists. The title must be unique.',
},
}),
{
status: 400,
statusText: 'Title is required',
},
);
}
}

return next(request, context);
34 changes: 24 additions & 10 deletions example/msw.ts
Original file line number Diff line number Diff line change
@@ -12,18 +12,32 @@ const restServer = new MswServer({
restServer.addMiddleware(withDelay(300));
restServer.addMiddleware(async (request, context, next) => {
if (!request.headers?.get('Authorization')) {
throw new HttpResponse(null, { status: 401 });
throw new Response(null, { status: 401 });
}
return next(request, context);
});

if (
context.collection === 'books' &&
request.method === 'POST' &&
!context.requestJson?.title
) {
throw new HttpResponse(null, {
status: 400,
statusText: 'Title is required',
});
restServer.addMiddleware(async (request, context, next) => {
if (context.collection === 'books' && request.method === 'POST') {
if (
restServer.collections[context.collection].getCount({
filter: {
title: context.requestJson?.title,
},
}) > 0
) {
throw new Response(
JSON.stringify({
errors: {
title: 'An article with this title already exists. The title must be unique.',
},
}),
{
status: 400,
statusText: 'Title is required',
},
);
}
}

return next(request, context);
24 changes: 24 additions & 0 deletions example/sinon.ts
Original file line number Diff line number Diff line change
@@ -20,6 +20,30 @@ export const initializeSinon = () => {
return next(request, context);
});

restServer.addMiddleware((request, context, next) => {
if (context.collection === 'books' && request.method === 'POST') {
if (
restServer.collections[context.collection].getCount({
filter: {
title: context.requestJson?.title,
},
}) > 0
) {
request.respond(
401,
{},
JSON.stringify({
errors: {
title: 'An article with this title already exists. The title must be unique.',
},
}),
);
}
}

return next(request, context);
});

// use sinon.js to monkey-patch XmlHttpRequest
const server = sinon.fakeServer.create();
// this is required when doing asynchronous XmlHttpRequest

0 comments on commit e2f4c19

Please sign in to comment.