Skip to content

Commit

Permalink
lib(validation-constraints): share validation constraints full-stack (#…
Browse files Browse the repository at this point in the history
…286)

* lib(validation-constraints): share validation constraints with the front-end

* test(validation-constraints): add unit test for isYear

* Add additional tests and test utils

* test(validation-constraints): add unit tests for constraint functions

* refactor(api): use validation-constraints in back-end

* build(validation-constraints): exclude test utils from the build

* Refactor based on PR (#286)
  • Loading branch information
aaron-plahn authored Jan 11, 2023
1 parent d4ea651 commit 80ca9de
Show file tree
Hide file tree
Showing 139 changed files with 42,986 additions and 45,102 deletions.
7 changes: 3 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ Thumbs.db

# front-end configs
auth_config.json
config.json
*.config.json
*.config.ts
configurable-front-matter.json
**content.config.ts
# TODO Be careful not to git-ignore third-party configs
**config.json
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"version": "0.2.0",
"configurations": [

{
"type": "node",
"request": "launch",
Expand Down Expand Up @@ -81,5 +82,20 @@
"console": "internalConsole",
"internalConsoleOptions": "neverOpen"
},
{
"type": "node",
"request": "launch",
"name": "Jest test Validation Constraints lib",
"program": "${workspaceFolder}/node_modules/nx/bin/nx.js",
"cwd": "${workspaceFolder}",
"args": [
"test",
"validation-constraints",
"--testFile=${file}",
"--runInBand",
],
"console": "internalConsole",
"internalConsoleOptions": "neverOpen"
},
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -1952,6 +1952,7 @@ Object {
"form": Object {
"fields": Array [
Object {
"constraints": Array [],
"description": "choose positive",
"label": "positive",
"name": "positive",
Expand All @@ -1968,6 +1969,7 @@ Object {
"type": "SWITCH",
},
Object {
"constraints": Array [],
"description": "choose person",
"label": "person",
"name": "person",
Expand Down Expand Up @@ -2023,6 +2025,7 @@ Object {
"form": Object {
"fields": Array [
Object {
"constraints": Array [],
"description": "choose person",
"label": "person",
"name": "person",
Expand Down Expand Up @@ -2113,6 +2116,7 @@ Object {
"form": Object {
"fields": Array [
Object {
"constraints": Array [],
"description": "choose his",
"label": "his",
"name": "his",
Expand Down Expand Up @@ -2179,6 +2183,7 @@ Object {
"form": Object {
"fields": Array [
Object {
"constraints": Array [],
"description": "choose positive",
"label": "positive",
"name": "positive",
Expand All @@ -2195,6 +2200,7 @@ Object {
"type": "SWITCH",
},
Object {
"constraints": Array [],
"description": "choose person",
"label": "person",
"name": "person",
Expand Down Expand Up @@ -2227,6 +2233,7 @@ Object {
"form": Object {
"fields": Array [
Object {
"constraints": Array [],
"description": "choose person",
"label": "person",
"name": "person",
Expand Down Expand Up @@ -2294,6 +2301,7 @@ Object {
"form": Object {
"fields": Array [
Object {
"constraints": Array [],
"description": "choose his",
"label": "his",
"name": "his",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ Object {
"form": Object {
"fields": Array [
Object {
"constraints": Array [],
"description": "choose positive",
"label": "positive",
"name": "positive",
Expand All @@ -432,6 +433,7 @@ Object {
"type": "SWITCH",
},
Object {
"constraints": Array [],
"description": "choose person",
"label": "person",
"name": "person",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ Object {
"form": Object {
"fields": Array [
Object {
"constraints": Array [
Object {
"message": "must be a UUID",
"name": "UUID",
},
],
"description": "the ID of the user that will be added to this group",
"label": "user ID",
"name": "userId",
Expand Down Expand Up @@ -273,6 +279,12 @@ Object {
"form": Object {
"fields": Array [
Object {
"constraints": Array [
Object {
"message": "must be a UUID",
"name": "UUID",
},
],
"description": "the ID of the user that will be added to this group",
"label": "user ID",
"name": "userId",
Expand Down Expand Up @@ -320,12 +332,24 @@ Object {
"form": Object {
"fields": Array [
Object {
"constraints": Array [
Object {
"message": "must be a non-empty string",
"name": "non-empty string",
},
],
"description": "the name of the user group",
"label": "label",
"name": "label",
"type": "TEXT_FIELD",
},
Object {
"constraints": Array [
Object {
"message": "must be a non-empty string",
"name": "non-empty string",
},
],
"description": "a summary of the purpose of this group (what its members have in common)",
"label": "description",
"name": "description",
Expand All @@ -349,7 +373,13 @@ Object {
"form": Object {
"fields": Array [
Object {
"description": "Add property description",
"constraints": Array [
Object {
"message": "Must be a valid \${propertyLabel}",
"name": "IS_ENUM",
},
],
"description": "the role that will be granted to this user",
"label": "User Role",
"name": "role",
"options": Array [
Expand Down Expand Up @@ -405,7 +435,13 @@ Object {
"form": Object {
"fields": Array [
Object {
"description": "Add property description",
"constraints": Array [
Object {
"message": "Must be a valid \${propertyLabel}",
"name": "IS_ENUM",
},
],
"description": "the role that will be granted to this user",
"label": "User Role",
"name": "role",
"options": Array [
Expand Down Expand Up @@ -456,12 +492,24 @@ Object {
"form": Object {
"fields": Array [
Object {
"constraints": Array [
Object {
"message": "must be a non-empty string",
"name": "non-empty string",
},
],
"description": "the auth provider's unique ID for this user (the user must be added to the auth provider first)",
"label": "auth provider's ID",
"name": "userIdFromAuthProvider",
"type": "TEXT_FIELD",
},
Object {
"constraints": Array [
Object {
"message": "must be a non-empty string",
"name": "non-empty string",
},
],
"description": "a human-readable text identifier for this user",
"label": "username",
"name": "username",
Expand All @@ -485,6 +533,12 @@ Object {
"form": Object {
"fields": Array [
Object {
"constraints": Array [
Object {
"message": "must be a UUID",
"name": "UUID",
},
],
"description": "the ID of the user that will be added to this group",
"label": "user ID",
"name": "userId",
Expand Down Expand Up @@ -537,6 +591,12 @@ Object {
"form": Object {
"fields": Array [
Object {
"constraints": Array [
Object {
"message": "must be a UUID",
"name": "UUID",
},
],
"description": "the ID of the user that will be added to this group",
"label": "user ID",
"name": "userId",
Expand Down Expand Up @@ -584,12 +644,24 @@ Object {
"form": Object {
"fields": Array [
Object {
"constraints": Array [
Object {
"message": "must be a non-empty string",
"name": "non-empty string",
},
],
"description": "the name of the user group",
"label": "label",
"name": "label",
"type": "TEXT_FIELD",
},
Object {
"constraints": Array [
Object {
"message": "must be a non-empty string",
"name": "non-empty string",
},
],
"description": "a summary of the purpose of this group (what its members have in common)",
"label": "description",
"name": "description",
Expand All @@ -613,7 +685,13 @@ Object {
"form": Object {
"fields": Array [
Object {
"description": "Add property description",
"constraints": Array [
Object {
"message": "Must be a valid \${propertyLabel}",
"name": "IS_ENUM",
},
],
"description": "the role that will be granted to this user",
"label": "User Role",
"name": "role",
"options": Array [
Expand Down Expand Up @@ -669,7 +747,13 @@ Object {
"form": Object {
"fields": Array [
Object {
"description": "Add property description",
"constraints": Array [
Object {
"message": "Must be a valid \${propertyLabel}",
"name": "IS_ENUM",
},
],
"description": "the role that will be granted to this user",
"label": "User Role",
"name": "role",
"options": Array [
Expand Down Expand Up @@ -720,12 +804,24 @@ Object {
"form": Object {
"fields": Array [
Object {
"constraints": Array [
Object {
"message": "must be a non-empty string",
"name": "non-empty string",
},
],
"description": "the auth provider's unique ID for this user (the user must be added to the auth provider first)",
"label": "auth provider's ID",
"name": "userIdFromAuthProvider",
"type": "TEXT_FIELD",
},
Object {
"constraints": Array [
Object {
"message": "must be a non-empty string",
"name": "non-empty string",
},
],
"description": "a human-readable text identifier for this user",
"label": "username",
"name": "username",
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/app/controllers/coscrad-user-group.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isStringWithNonzeroLength } from '@coscrad/validation';
import { isNonEmptyString } from '@coscrad/validation-constraints';
import { Controller, Get, Param, Res, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { CoscradUserGroupQueryService } from '../../domain/services/query-services/coscrad-user-group-query.service';
Expand All @@ -16,7 +16,7 @@ export class CoscradUserGroupController {
@UseGuards(AdminJwtGuard)
@Get('/:id')
async fetchById(@Res() res, @Param('id') id: string) {
if (!isStringWithNonzeroLength(id))
if (!isNonEmptyString(id))
return sendInternalResultAsHttpResponse(
res,
new InternalError(`Invalid group ID: ${id}`)
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/app/controllers/coscrad-user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isStringWithNonzeroLength } from '@coscrad/validation';
import { isNonEmptyString } from '@coscrad/validation-constraints';
import { Controller, Get, Param, Res, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { CoscradUserQueryService } from '../../domain/services/query-services/coscrad-user-query.service';
Expand All @@ -16,7 +16,7 @@ export class CoscradUserController {
@UseGuards(AdminJwtGuard)
@Get('/:id')
async fetchById(@Res() res, @Param('id') id: string) {
if (!isStringWithNonzeroLength(id))
if (!isNonEmptyString(id))
return sendInternalResultAsHttpResponse(
res,
new InternalError(`Invalid user ID: ${id}`)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isStringWithNonzeroLength } from '@coscrad/validation';
import { isNonEmptyString } from '@coscrad/validation-constraints';
import { AggregateType } from '../../../domain/types/AggregateType';
import { isResourceType, ResourceType } from '../../../domain/types/ResourceType';
import { InternalError } from '../../../lib/errors/InternalError';
Expand Down Expand Up @@ -26,7 +26,7 @@ export const buildIndexPathForAggregate = (aggregateType: AggregateType): string

const lookupResult = nonResourceAggregateTypeToIndexPath[aggregateType];

if (!isStringWithNonzeroLength(lookupResult)) {
if (!isNonEmptyString(lookupResult)) {
throw new InternalError(
`There is no route registered for aggregate of type: ${aggregateType}`
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
NonNegativeFiniteNumber,
URL,
} from '@coscrad/data-types';
import { isStringWithNonzeroLength } from '@coscrad/validation';
import { isNonEmptyString } from '@coscrad/validation-constraints';
import { RegisterIndexScopedCommands } from '../../../../app/controllers/command/command-info/decorators/register-index-scoped-commands.decorator';
import { InternalError } from '../../../../lib/errors/InternalError';
import { DTO } from '../../../../types/DTO';
Expand Down Expand Up @@ -95,7 +95,7 @@ export class MediaItem extends Resource implements ITimeBoundable {

const allErrors: InternalError[] = [];

if (!isStringWithNonzeroLength(title) && !isStringWithNonzeroLength(titleEnglish))
if (!isNonEmptyString(title) && !isNonEmptyString(titleEnglish))
allErrors.push(new MediaItemHasNoTitleInAnyLanguageError(id));

return allErrors;
Expand Down
Loading

0 comments on commit 80ca9de

Please sign in to comment.