Skip to content

Relationship uuid support #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions libs/json-api-nestjs/src/lib/config/bindings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const Bindings: BindingsConfig = {
{
property: PARAMS_RESOURCE_ID,
decorator: Param,
mixins: [idPipeMixin],
mixins: [],
},
{
decorator: Query,
Expand All @@ -72,7 +72,7 @@ const Bindings: BindingsConfig = {
{
property: PARAMS_RESOURCE_ID,
decorator: Param,
mixins: [idPipeMixin],
mixins: [],
},
],
},
Expand All @@ -97,7 +97,7 @@ const Bindings: BindingsConfig = {
{
property: PARAMS_RESOURCE_ID,
decorator: Param,
mixins: [idPipeMixin],
mixins: [],
},
{
decorator: Body,
Expand All @@ -114,7 +114,7 @@ const Bindings: BindingsConfig = {
{
property: PARAMS_RESOURCE_ID,
decorator: Param,
mixins: [idPipeMixin],
mixins: [],
},
{
property: PARAMS_RELATION_NAME,
Expand All @@ -132,7 +132,7 @@ const Bindings: BindingsConfig = {
{
property: PARAMS_RESOURCE_ID,
decorator: Param,
mixins: [idPipeMixin],
mixins: [],
},
{
property: PARAMS_RELATION_NAME,
Expand All @@ -154,7 +154,7 @@ const Bindings: BindingsConfig = {
{
property: PARAMS_RESOURCE_ID,
decorator: Param,
mixins: [idPipeMixin],
mixins: [],
},
{
property: PARAMS_RELATION_NAME,
Expand All @@ -176,7 +176,7 @@ const Bindings: BindingsConfig = {
{
property: PARAMS_RESOURCE_ID,
decorator: Param,
mixins: [idPipeMixin],
mixins: [],
},
{
property: PARAMS_RELATION_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export function inputBodyPostSchema(
camelToKebab(getEntityName(entity))
);

const relDataType = {
const relDataType: Record<string, any> = {
type: 'object',
properties: {
data: {
Expand All @@ -34,8 +34,6 @@ export function inputBodyPostSchema(
},
id: {
type: 'string',
pattern: '^\\d+$',
description: 'Use string should be as number string',
},
},
required: ['type', 'id'],
Expand Down Expand Up @@ -104,6 +102,14 @@ export function inputBodyPostSchema(
};

const relationships = Object.keys(relationsField).reduce((acum, item) => {
if (
Copy link
Owner

@klerick klerick Mar 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, better use json schema validate uuid.
You can use additional conditional before get from metadata information about ID.
like this:

if(isUuid){
relDataType.properties.data.properties.id.pattern = '^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$'
relDataType.properties.data.properties.id.minLength = 36
relDataType.properties.data.properties.id.maxLength = 36
}

so, before it, not need check in pipe

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, I'll update my proposal later this week.

Reflect.getMetadata('design:type', entity['prototype'], item) === Number
) {
relDataType.properties.data.properties.id.pattern = '^\\d+$';
relDataType.properties.data.properties.id.description =
'Use string should be as number string';
}

const resultSchema = {
...relDataType.properties.data,
properties: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@nestjs/common';
import { Repository } from 'typeorm';
import AjvCall, { ValidateFunction } from 'ajv';
import { validate as classValidate } from 'class-validator';
import { isUUID, validate as classValidate } from 'class-validator';

import { PipeMixin, ValidationError } from '../../../types';
import { ResourceRequestObject } from '../../../types-common/request';
Expand Down Expand Up @@ -104,6 +104,29 @@ export class BodyInputPostPipe<Entity> implements PipeTransform {
throw new UnprocessableEntityException(errorResult);
}

const relationsMetadata = this.repository.metadata.relations;

for (const relationMetadata of relationsMetadata) {
const targetMetadata = relationMetadata.inverseEntityMetadata;
const primaryColumn = targetMetadata.primaryColumns[0];
const isUuidPrimaryKey = primaryColumn.generationStrategy === 'uuid';

if (isUuidPrimaryKey) {
const valid = isUUID(
value.data.relationships[relationMetadata.propertyName].data.id
);

if (!valid) {
throw new UnprocessableEntityException({
source: {
pointer: `/data/relationships/${relationMetadata.propertyName}/id`,
},
detail: `Invalid UUID`,
});
}
}
}

const dateKey = Object.keys(value.data.attributes).filter(
(i) =>
Reflect.getMetadata(
Expand Down