@contember/schema-utils
Advanced tools
Comparing version 0.10.0-alpha.7 to 0.10.0-alpha.8
@@ -6,3 +6,4 @@ "use strict"; | ||
exports.resolveDefaultValue = (column, providers) => { | ||
switch (column.type) { | ||
const type = column.type; | ||
switch (type) { | ||
case schema_1.Model.ColumnType.String: | ||
@@ -13,2 +14,3 @@ case schema_1.Model.ColumnType.Int: | ||
case schema_1.Model.ColumnType.Bool: | ||
case schema_1.Model.ColumnType.Json: | ||
if (typeof column.default !== 'undefined') { | ||
@@ -28,3 +30,3 @@ return column.default; | ||
; | ||
((x) => { })(column); | ||
((x) => { })(type); | ||
} | ||
@@ -31,0 +33,0 @@ if (column.nullable) { |
@@ -14,3 +14,4 @@ import { Model } from '@contember/schema'; | ||
private validateIdentifier; | ||
private validateCollisions; | ||
} | ||
//# sourceMappingURL=ModelValidator.d.ts.map |
@@ -27,2 +27,3 @@ "use strict"; | ||
} | ||
this.validateCollisions(Object.values(validModel.entities), errorBuilder.for('entities')); | ||
return [validModel, errorBuilder.errors]; | ||
@@ -324,2 +325,39 @@ } | ||
} | ||
validateCollisions(entities, errorBuilder) { | ||
const tableNames = {}; | ||
for (const entity of entities) { | ||
const description = `entity ${entity.name}`; | ||
if (tableNames[entity.tableName]) { | ||
errorBuilder | ||
.for(entity.name) | ||
.add(`Table name ${entity.tableName} of ${description} collides with a table name of ${tableNames[entity.tableName]}`); | ||
} | ||
else { | ||
tableNames[entity.tableName] = description; | ||
} | ||
} | ||
for (const entity of entities) { | ||
const entityErrorBuilder = errorBuilder.for(entity.name); | ||
model_1.acceptEveryFieldVisitor(this.model, entity, { | ||
visitManyHasManyOwning: (entity, relation) => { | ||
const joiningTable = relation.joiningTable; | ||
const description = `relation ${entity.name}::${relation.name}`; | ||
if (tableNames[joiningTable.tableName]) { | ||
entityErrorBuilder | ||
.for(relation.name) | ||
.add(`Joining table name ${joiningTable.tableName} of ${description} collides with a table name of ${tableNames[joiningTable.tableName]}. Consider using plural for a relation name or change the joining table name using .joiningTable(...) in schema definition.`); | ||
} | ||
else { | ||
tableNames[joiningTable.tableName] = description; | ||
} | ||
}, | ||
visitColumn: () => { }, | ||
visitManyHasManyInverse: () => { }, | ||
visitOneHasMany: () => { }, | ||
visitOneHasOneInverse: () => { }, | ||
visitOneHasOneOwning: () => { }, | ||
visitManyHasOne: () => { }, | ||
}); | ||
} | ||
} | ||
} | ||
@@ -326,0 +364,0 @@ exports.ModelValidator = ModelValidator; |
{ | ||
"name": "@contember/schema-utils", | ||
"version": "0.10.0-alpha.7", | ||
"version": "0.10.0-alpha.8", | ||
"license": "Apache-2.0", | ||
@@ -11,3 +11,3 @@ "main": "dist/src/index.js", | ||
"dependencies": { | ||
"@contember/schema": "^0.10.0-alpha.7" | ||
"@contember/schema": "^0.10.0-alpha.8" | ||
}, | ||
@@ -14,0 +14,0 @@ "devDependencies": { |
@@ -9,3 +9,4 @@ import { Input, Model, Value } from '@contember/schema' | ||
export const resolveDefaultValue = (column: Model.AnyColumn, providers: Pick<Providers, 'now'>) => { | ||
switch (column.type) { | ||
const type = column.type | ||
switch (type) { | ||
case Model.ColumnType.String: | ||
@@ -16,2 +17,3 @@ case Model.ColumnType.Int: | ||
case Model.ColumnType.Bool: | ||
case Model.ColumnType.Json: | ||
if (typeof column.default !== 'undefined') { | ||
@@ -30,3 +32,3 @@ return column.default | ||
default: | ||
;((x: never) => {})(column) | ||
;((x: never) => {})(type) | ||
} | ||
@@ -33,0 +35,0 @@ |
import { Model } from '@contember/schema' | ||
import { ErrorBuilder, ValidationError } from './errors' | ||
import { everyIs, isObject, UnknownObject } from './utils' | ||
import { getTargetEntity, isInverseRelation, isOwningRelation } from '../model' | ||
import { acceptEveryFieldVisitor, getTargetEntity, isInverseRelation, isOwningRelation } from '../model' | ||
@@ -24,2 +24,3 @@ const IDENTIFIER_PATTERN = /^[_a-zA-Z][_a-zA-Z0-9]*$/ | ||
} | ||
this.validateCollisions(Object.values(validModel.entities), errorBuilder.for('entities')) | ||
return [validModel, errorBuilder.errors] | ||
@@ -352,2 +353,46 @@ } | ||
} | ||
private validateCollisions(entities: Model.Entity[], errorBuilder: ErrorBuilder) { | ||
const tableNames: Record<string, string> = {} | ||
for (const entity of entities) { | ||
const description = `entity ${entity.name}` | ||
if (tableNames[entity.tableName]) { | ||
errorBuilder | ||
.for(entity.name) | ||
.add( | ||
`Table name ${entity.tableName} of ${description} collides with a table name of ${ | ||
tableNames[entity.tableName] | ||
}`, | ||
) | ||
} else { | ||
tableNames[entity.tableName] = description | ||
} | ||
} | ||
for (const entity of entities) { | ||
const entityErrorBuilder = errorBuilder.for(entity.name) | ||
acceptEveryFieldVisitor(this.model, entity, { | ||
visitManyHasManyOwning: (entity, relation) => { | ||
const joiningTable = relation.joiningTable | ||
const description = `relation ${entity.name}::${relation.name}` | ||
if (tableNames[joiningTable.tableName]) { | ||
entityErrorBuilder | ||
.for(relation.name) | ||
.add( | ||
`Joining table name ${joiningTable.tableName} of ${description} collides with a table name of ${ | ||
tableNames[joiningTable.tableName] | ||
}. Consider using plural for a relation name or change the joining table name using .joiningTable(...) in schema definition.`, | ||
) | ||
} else { | ||
tableNames[joiningTable.tableName] = description | ||
} | ||
}, | ||
visitColumn: () => {}, | ||
visitManyHasManyInverse: () => {}, | ||
visitOneHasMany: () => {}, | ||
visitOneHasOneInverse: () => {}, | ||
visitOneHasOneOwning: () => {}, | ||
visitManyHasOne: () => {}, | ||
}) | ||
} | ||
} | ||
} | ||
@@ -354,0 +399,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
427191
3643