Skip to content
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

Do not add non-existent relationship properties #90

Merged
merged 1 commit into from
Jan 15, 2024
Merged
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
2 changes: 2 additions & 0 deletions documentation/md/docs/Models/Defining-a-Model.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ A sample Model definition with all configuration options is the following. Note
> > /* --> schema validation for it */
> > schema: {
> > type: 'number',
> > required: true,
> > },
> > },
> > }
Expand Down Expand Up @@ -186,6 +187,7 @@ A sample Model definition with all configuration options is the following. Note
> > /* --> schema validation for it */
> > schema: {
> > type: 'number',
> > required: true,
> > },
> > },
> > }
Expand Down
24 changes: 23 additions & 1 deletion src/ModelOps/ModelOps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ describe('ModelFactory', () => {
age: {
type: 'number',
minimum: 0,
required: false,
},
id: {
type: 'string',
Expand All @@ -157,6 +158,7 @@ describe('ModelFactory', () => {
type: 'number',
minimum: 1,
maximum: 5,
required: true,
},
},
},
Expand Down Expand Up @@ -424,6 +426,7 @@ describe('createOne', () => {
property: 'rating',
schema: {
type: 'number',
required: true,
},
},
},
Expand Down Expand Up @@ -818,6 +821,7 @@ describe('addRelationships', () => {
type: 'number',
minimum: 0,
maximum: 5,
required: true,
},
},
},
Expand All @@ -840,6 +844,7 @@ describe('addRelationships', () => {
property: 'more',
schema: {
type: 'boolean',
required: true,
},
},
},
Expand Down Expand Up @@ -1260,6 +1265,7 @@ describe('beforeCreate', () => {
type: 'number',
minimum: 0,
maximum: 5,
required: true,
},
},
},
Expand Down Expand Up @@ -1571,7 +1577,13 @@ describe('relateTo', () => {
interface UsersRelatedNodesI {
Parent: ModelRelatedNodesI<
{ createOne: (typeof Users)['createOne'] },
UsersInstance
UsersInstance,
{
Rating?: number;
},
{
rating?: number;
}
>;
}

Expand Down Expand Up @@ -1614,6 +1626,15 @@ describe('relateTo', () => {
model: 'self',
direction: 'out',
name: 'PARENT',
properties: {
Rating: {
property: 'rating',
schema: {
type: 'number',
required: false,
},
},
},
},
},
primaryKeyField: 'id',
Expand Down Expand Up @@ -1704,6 +1725,7 @@ describe('relateTo', () => {
type: 'number',
minimum: 0,
maximum: 5,
required: true,
},
},
},
Expand Down
24 changes: 14 additions & 10 deletions src/ModelOps/ModelOps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ type RelationshipTypePropertyForCreateI<
| Array<RelationshipTypePropertyForCreateWhereI<RelationshipProperties>>;
};

type IValidationSchema<T = AnyObject> = Revalidator.ISchema<T> & {
required: boolean;
};

/** the type for the Relationship configuration of a Model */
export type RelationshipsI<RelatedNodesToAssociateI extends AnyObject> = {
/** the alias of the relationship definitions is the key */
Expand All @@ -118,7 +122,7 @@ export type RelationshipsI<RelatedNodesToAssociateI extends AnyObject> = {
/** the actual property to be used on the relationship */
property: keyof RelatedNodesToAssociateI[alias]['RelationshipProperties'];
/** validation for the property */
schema: Revalidator.ISchema<AnyObject>;
schema: IValidationSchema;
};
};
};
Expand Down Expand Up @@ -416,7 +420,7 @@ export const ModelFactory = <
/** the schema for the validation */
schema: {
[index in keyof Properties]:
| Revalidator.ISchema<Properties>
| IValidationSchema<Properties>
| Revalidator.JSONSchema<Properties>;
};
/** the label of the nodes */
Expand Down Expand Up @@ -1689,24 +1693,24 @@ export const ModelFactory = <
/** properties to be used in the relationship */
const relationshipProperties = {};
/** total validation for the properties */
const validationSchema: Record<
string,
Revalidator.ISchema<AnyObject>
> = {};
const validationSchema: Record<string, IValidationSchema<AnyObject>> = {};

for (const key of keysToUse) {
const property = relationship.properties?.[key]?.property as string;

if (!property) {
continue;
}
relationshipProperties[property] = dataToUse[key];

const schema = relationship.properties?.[key]?.schema;
if (!schema) {
continue;

if (schema) {
validationSchema[property] = schema;
}

if (key in dataToUse) {
relationshipProperties[property] = dataToUse[key];
}
validationSchema[property] = schema;
}

const validationResult = revalidator.validate(relationshipProperties, {
Expand Down