🚀 Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more
Socket
DemoInstallSign in
Socket

@contember/schema-utils

Package Overview
Dependencies
Maintainers
0
Versions
304
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@contember/schema-utils - npm Package Compare versions

Comparing version

to
2.1.0-alpha.1

3

dist/development/index.js

@@ -21,3 +21,3 @@ import { emptyModelSchema } from "./src/model/modelUtils.js";

import { getRoleVariables } from "./src/acl/schemaUtils.js";
import { NoDataError, resolveColumnValue, resolveDefaultValue, resolvePrimaryGenerator } from "./src/dataUtils.js";
import { NoDataError, resolveColumnValue, resolveDefaultValue } from "./src/dataUtils.js";
import { AclValidator } from "./src/validation/AclValidator.js";

@@ -92,3 +92,2 @@ import { ModelValidator } from "./src/validation/ModelValidator.js";

resolveDefaultValue,
resolvePrimaryGenerator,
schemaType,

@@ -95,0 +94,0 @@ tryGetColumnName

@@ -20,5 +20,9 @@ import { Model } from "@contember/schema";

case Model.ColumnType.Date:
case Model.ColumnType.Time:
if (column.default === "now") {
return providers.now().toISOString();
}
if (typeof column.default !== "undefined") {
return column.default;
}
break;

@@ -33,8 +37,2 @@ case Model.ColumnType.Uuid:

};
const resolvePrimaryGenerator = (column, providers) => {
if (column.type === Model.ColumnType.Uuid) {
return providers.uuid;
}
return () => void 0;
};
const resolveColumnValue = ({

@@ -44,3 +42,3 @@ entity,

input
}, providers) => {
}, providers, options) => {
if (input !== void 0) {

@@ -50,3 +48,6 @@ return input;

if (entity.primary === column.name) {
return resolvePrimaryGenerator(column, providers)();
if (column.type === Model.ColumnType.Uuid) {
return providers.uuid({ version: options.uuidVersion });
}
return void 0;
}

@@ -60,5 +61,4 @@ return resolveDefaultValue(column, providers);

resolveColumnValue,
resolveDefaultValue,
resolvePrimaryGenerator
resolveDefaultValue
};
//# sourceMappingURL=dataUtils.js.map

@@ -208,2 +208,3 @@ import { Model } from "@contember/schema";

[Model.ColumnType.DateTime]: "dateTimeColumn",
[Model.ColumnType.Time]: "timeColumn",
[Model.ColumnType.Json]: "jsonColumn",

@@ -210,0 +211,0 @@ [Model.ColumnType.Double]: "doubleColumn",

@@ -60,3 +60,3 @@ import { assertNever } from "../utils/assertNever.js";

// TODO solve enum handling properly maybe we should distinguish between domain and column type
visitColumn: ({ column }) => column.type === Model.ColumnType.Enum ? "text" : column.columnType,
visitColumn: ({ column }) => (column.type === Model.ColumnType.Enum ? "text" : column.columnType) + (column.list ? "[]" : ""),
visitRelation: ({ entity: entity2, relation, targetEntity }) => {

@@ -63,0 +63,0 @@ if (isIt(relation, "joiningColumn")) {

@@ -19,2 +19,4 @@ import { Model } from "@contember/schema";

return "date";
case Model.ColumnType.Time:
return "time";
case Model.ColumnType.Json:

@@ -21,0 +23,0 @@ return "jsonb";

@@ -55,2 +55,3 @@ import * as Typesafe from "@contember/typesafe";

case Model.ColumnType.DateTime:
case Model.ColumnType.Time:
case Model.ColumnType.Uuid:

@@ -57,0 +58,0 @@ case Model.ColumnType.Enum:

@@ -114,3 +114,4 @@ import * as Typesafe from "@contember/typesafe";

typeAlias: Typesafe.string,
default: Typesafe.union(Typesafe.string, Typesafe.number, Typesafe.boolean, Typesafe.null_),
list: Typesafe.boolean,
default: Typesafe.anyJson,
sequence: intersectionSchema

@@ -117,0 +118,0 @@ })

@@ -10,3 +10,4 @@ import * as Typesafe from "@contember/typesafe";

fullDateTimeResponse: Typesafe.boolean,
shortDateResponse: Typesafe.boolean
shortDateResponse: Typesafe.boolean,
uuidVersion: Typesafe.union(Typesafe.literal(4), Typesafe.literal(7))
})

@@ -13,0 +14,0 @@ });

import { Model } from "@contember/schema";
import { ErrorBuilder } from "./errors.js";
import { acceptEveryFieldVisitor, getTargetEntity, isInverseRelation, isOwningRelation } from "../model/modelUtils.js";
import { isColumn, acceptEveryFieldVisitor, getTargetEntity, isInverseRelation, isOwningRelation } from "../model/modelUtils.js";
const IDENTIFIER_PATTERN = /^[_a-zA-Z][_a-zA-Z0-9]*$/;

@@ -48,4 +48,3 @@ const RESERVED_WORDS = ["and", "or", "not"];

this.validateUniqueConstraints(
entity.unique,
new Set(Object.keys(entity.fields)),
entity,
errors.for("unique")

@@ -55,7 +54,10 @@ );

}
validateUniqueConstraints(uniqueConstraints, fields, errors) {
for (const constraint of uniqueConstraints) {
validateUniqueConstraints(entity, errors) {
for (const constraint of entity.unique) {
for (const field of constraint.fields) {
if (!fields.has(field)) {
const fieldDef = entity.fields[field];
if (!fieldDef) {
errors.add("MODEL_UNDEFINED_FIELD", `Referenced field ${field} in a constraint does not exists`);
} else if (isColumn(fieldDef) && fieldDef.list) {
errors.add("MODEL_INVALID_FIELD", `Field ${field} in a unique constraint cannot be an array`);
}

@@ -62,0 +64,0 @@ }

@@ -21,3 +21,3 @@ import { emptyModelSchema } from "./src/model/modelUtils.js";

import { getRoleVariables } from "./src/acl/schemaUtils.js";
import { NoDataError, resolveColumnValue, resolveDefaultValue, resolvePrimaryGenerator } from "./src/dataUtils.js";
import { NoDataError, resolveColumnValue, resolveDefaultValue } from "./src/dataUtils.js";
import { AclValidator } from "./src/validation/AclValidator.js";

@@ -92,3 +92,2 @@ import { ModelValidator } from "./src/validation/ModelValidator.js";

resolveDefaultValue,
resolvePrimaryGenerator,
schemaType,

@@ -95,0 +94,0 @@ tryGetColumnName

@@ -20,5 +20,9 @@ import { Model } from "@contember/schema";

case Model.ColumnType.Date:
case Model.ColumnType.Time:
if (column.default === "now") {
return providers.now().toISOString();
}
if (typeof column.default !== "undefined") {
return column.default;
}
break;

@@ -33,8 +37,2 @@ case Model.ColumnType.Uuid:

};
const resolvePrimaryGenerator = (column, providers) => {
if (column.type === Model.ColumnType.Uuid) {
return providers.uuid;
}
return () => void 0;
};
const resolveColumnValue = ({

@@ -44,3 +42,3 @@ entity,

input
}, providers) => {
}, providers, options) => {
if (input !== void 0) {

@@ -50,3 +48,6 @@ return input;

if (entity.primary === column.name) {
return resolvePrimaryGenerator(column, providers)();
if (column.type === Model.ColumnType.Uuid) {
return providers.uuid({ version: options.uuidVersion });
}
return void 0;
}

@@ -60,5 +61,4 @@ return resolveDefaultValue(column, providers);

resolveColumnValue,
resolveDefaultValue,
resolvePrimaryGenerator
resolveDefaultValue
};
//# sourceMappingURL=dataUtils.js.map

@@ -208,2 +208,3 @@ import { Model } from "@contember/schema";

[Model.ColumnType.DateTime]: "dateTimeColumn",
[Model.ColumnType.Time]: "timeColumn",
[Model.ColumnType.Json]: "jsonColumn",

@@ -210,0 +211,0 @@ [Model.ColumnType.Double]: "doubleColumn",

@@ -60,3 +60,3 @@ import { assertNever } from "../utils/assertNever.js";

// TODO solve enum handling properly maybe we should distinguish between domain and column type
visitColumn: ({ column }) => column.type === Model.ColumnType.Enum ? "text" : column.columnType,
visitColumn: ({ column }) => (column.type === Model.ColumnType.Enum ? "text" : column.columnType) + (column.list ? "[]" : ""),
visitRelation: ({ entity: entity2, relation, targetEntity }) => {

@@ -63,0 +63,0 @@ if (isIt(relation, "joiningColumn")) {

@@ -19,2 +19,4 @@ import { Model } from "@contember/schema";

return "date";
case Model.ColumnType.Time:
return "time";
case Model.ColumnType.Json:

@@ -21,0 +23,0 @@ return "jsonb";

@@ -55,2 +55,3 @@ import * as Typesafe from "@contember/typesafe";

case Model.ColumnType.DateTime:
case Model.ColumnType.Time:
case Model.ColumnType.Uuid:

@@ -57,0 +58,0 @@ case Model.ColumnType.Enum:

@@ -114,3 +114,4 @@ import * as Typesafe from "@contember/typesafe";

typeAlias: Typesafe.string,
default: Typesafe.union(Typesafe.string, Typesafe.number, Typesafe.boolean, Typesafe.null_),
list: Typesafe.boolean,
default: Typesafe.anyJson,
sequence: intersectionSchema

@@ -117,0 +118,0 @@ })

@@ -10,3 +10,4 @@ import * as Typesafe from "@contember/typesafe";

fullDateTimeResponse: Typesafe.boolean,
shortDateResponse: Typesafe.boolean
shortDateResponse: Typesafe.boolean,
uuidVersion: Typesafe.union(Typesafe.literal(4), Typesafe.literal(7))
})

@@ -13,0 +14,0 @@ });

import { Model } from "@contember/schema";
import { ErrorBuilder } from "./errors.js";
import { acceptEveryFieldVisitor, getTargetEntity, isInverseRelation, isOwningRelation } from "../model/modelUtils.js";
import { isColumn, acceptEveryFieldVisitor, getTargetEntity, isInverseRelation, isOwningRelation } from "../model/modelUtils.js";
const IDENTIFIER_PATTERN = /^[_a-zA-Z][_a-zA-Z0-9]*$/;

@@ -48,4 +48,3 @@ const RESERVED_WORDS = ["and", "or", "not"];

this.validateUniqueConstraints(
entity.unique,
new Set(Object.keys(entity.fields)),
entity,
errors.for("unique")

@@ -55,7 +54,10 @@ );

}
validateUniqueConstraints(uniqueConstraints, fields, errors) {
for (const constraint of uniqueConstraints) {
validateUniqueConstraints(entity, errors) {
for (const constraint of entity.unique) {
for (const field of constraint.fields) {
if (!fields.has(field)) {
const fieldDef = entity.fields[field];
if (!fieldDef) {
errors.add("MODEL_UNDEFINED_FIELD", `Referenced field ${field} in a constraint does not exists`);
} else if (isColumn(fieldDef) && fieldDef.list) {
errors.add("MODEL_INVALID_FIELD", `Field ${field} in a unique constraint cannot be an array`);
}

@@ -62,0 +64,0 @@ }

import { Input, Model, Value } from '@contember/schema';
export interface Providers {
uuid: () => string;
uuid: (args?: {
version?: 4 | 7;
}) => string;
now: () => Date;
}
export declare const resolveDefaultValue: (column: Model.AnyColumn, providers: Pick<Providers, "now">) => string | number | boolean | null | undefined;
export declare const resolvePrimaryGenerator: (column: Model.AnyColumn, providers: Providers) => (() => Input.PrimaryValue | undefined);
export declare const resolveDefaultValue: (column: Model.AnyColumn, providers: Pick<Providers, "now">) => import("@contember/schema").JSONValue | undefined;
export declare const resolveColumnValue: ({ entity, column, input, }: {

@@ -12,5 +13,7 @@ entity: Model.Entity;

input: Input.ColumnValue | undefined;
}, providers: Providers) => Value.FieldValue | undefined;
}, providers: Providers, options: {
uuidVersion?: 4 | 7;
}) => Value.FieldValue | undefined;
export declare class NoDataError extends Error {
}
//# sourceMappingURL=dataUtils.d.ts.map

@@ -12,2 +12,3 @@ import * as Typesafe from '@contember/typesafe';

readonly shortDateResponse?: boolean | undefined;
readonly uuidVersion?: 4 | 7 | undefined;
} | undefined;

@@ -30,2 +31,3 @@ };

readonly shortDateResponse?: boolean | undefined;
readonly uuidVersion?: 4 | 7 | undefined;
};

@@ -36,2 +38,3 @@ inner: {

shortDateResponse: Typesafe.Type<boolean>;
uuidVersion: Typesafe.Type<4 | 7>;
};

@@ -38,0 +41,0 @@ };

@@ -1,2 +0,2 @@

export type ValidationErrorCode = 'MODEL_NAME_MISMATCH' | 'MODEL_UNDEFINED_FIELD' | 'MODEL_UNDEFINED_ENTITY' | 'MODEL_RELATION_REQUIRED' | 'MODEL_INVALID_RELATION_DEFINITION' | 'MODEL_INVALID_COLUMN_DEFINITION' | 'MODEL_INVALID_VIEW_USAGE' | 'MODEL_INVALID_IDENTIFIER' | 'MODEL_INVALID_ENTITY_NAME' | 'MODEL_NAME_COLLISION' | 'ACL_INVALID_CONDITION' | 'ACL_UNDEFINED_VARIABLE' | 'ACL_UNDEFINED_FIELD' | 'ACL_UNDEFINED_PREDICATE' | 'ACL_UNDEFINED_ROLE' | 'ACL_UNDEFINED_ENTITY' | 'ACTIONS_NAME_MISMATCH' | 'ACTIONS_INVALID_CONDITION' | 'ACTIONS_UNDEFINED_FIELD' | 'ACTIONS_UNDEFINED_ENTITY' | 'ACTIONS_UNDEFINED_TRIGGER_TARGET' | 'ACTIONS_INVALID_SELECTION' | 'VALIDATION_UNDEFINED_ENTITY' | 'VALIDATION_UNDEFINED_FIELD' | 'VALIDATION_NOT_IMPLEMENTED';
export type ValidationErrorCode = 'MODEL_NAME_MISMATCH' | 'MODEL_UNDEFINED_FIELD' | 'MODEL_INVALID_FIELD' | 'MODEL_UNDEFINED_ENTITY' | 'MODEL_RELATION_REQUIRED' | 'MODEL_INVALID_RELATION_DEFINITION' | 'MODEL_INVALID_COLUMN_DEFINITION' | 'MODEL_INVALID_VIEW_USAGE' | 'MODEL_INVALID_IDENTIFIER' | 'MODEL_INVALID_ENTITY_NAME' | 'MODEL_NAME_COLLISION' | 'ACL_INVALID_CONDITION' | 'ACL_UNDEFINED_VARIABLE' | 'ACL_UNDEFINED_FIELD' | 'ACL_UNDEFINED_PREDICATE' | 'ACL_UNDEFINED_ROLE' | 'ACL_UNDEFINED_ENTITY' | 'ACTIONS_NAME_MISMATCH' | 'ACTIONS_INVALID_CONDITION' | 'ACTIONS_UNDEFINED_FIELD' | 'ACTIONS_UNDEFINED_ENTITY' | 'ACTIONS_UNDEFINED_TRIGGER_TARGET' | 'ACTIONS_INVALID_SELECTION' | 'VALIDATION_UNDEFINED_ENTITY' | 'VALIDATION_UNDEFINED_FIELD' | 'VALIDATION_NOT_IMPLEMENTED';
export interface ValidationError {

@@ -3,0 +3,0 @@ path: (string | number)[];

{
"name": "@contember/schema-utils",
"version": "2.0.0",
"version": "2.1.0-alpha.1",
"license": "Apache-2.0",

@@ -26,7 +26,7 @@ "main": "./dist/production/index.js",

"dependencies": {
"@contember/schema": "2.0.0",
"@contember/typesafe": "2.0.0"
"@contember/schema": "2.1.0-alpha.1",
"@contember/typesafe": "2.1.0-alpha.1"
},
"devDependencies": {
"@contember/schema-definition": "2.0.0",
"@contember/schema-definition": "2.1.0-alpha.1",
"@types/node": "^20.16.11"

@@ -33,0 +33,0 @@ },

import { Input, Model, Value } from '@contember/schema'
export interface Providers {
uuid: () => string
uuid: (args?: { version?: 4 | 7 }) => string
now: () => Date

@@ -28,5 +28,9 @@ }

case Model.ColumnType.Date:
case Model.ColumnType.Time:
if (column.default === 'now') {
return providers.now().toISOString()
}
if (typeof column.default !== 'undefined') {
return column.default
}
break

@@ -46,9 +50,2 @@ case Model.ColumnType.Uuid:

export const resolvePrimaryGenerator = (column: Model.AnyColumn, providers: Providers): (() => Input.PrimaryValue | undefined) => {
if (column.type === Model.ColumnType.Uuid) {
return providers.uuid
}
return () => undefined
}
export const resolveColumnValue = (

@@ -65,2 +62,5 @@ {

providers: Providers,
options: {
uuidVersion?: 4 | 7
},
): Value.FieldValue | undefined => {

@@ -71,3 +71,6 @@ if (input !== undefined) {

if (entity.primary === column.name) {
return resolvePrimaryGenerator(column, providers)()
if (column.type === Model.ColumnType.Uuid) {
return providers.uuid({ version: options.uuidVersion })
}
return undefined
}

@@ -74,0 +77,0 @@

@@ -237,2 +237,3 @@ import { Model, Schema, Writable } from '@contember/schema'

[Model.ColumnType.DateTime]: 'dateTimeColumn',
[Model.ColumnType.Time]: 'timeColumn',
[Model.ColumnType.Json]: 'jsonColumn',

@@ -239,0 +240,0 @@ [Model.ColumnType.Double]: 'doubleColumn',

@@ -68,3 +68,3 @@ import { assertNever, isIt } from '../utils'

// TODO solve enum handling properly maybe we should distinguish between domain and column type
visitColumn: ({ column }) => (column.type === Model.ColumnType.Enum ? 'text' : column.columnType),
visitColumn: ({ column }) => (column.type === Model.ColumnType.Enum ? 'text' : column.columnType) + (column.list ? '[]' : ''),
visitRelation: ({ entity, relation, targetEntity }) => {

@@ -71,0 +71,0 @@ if (isIt<Model.JoiningColumnRelation>(relation, 'joiningColumn')) {

@@ -20,2 +20,4 @@ import { Model } from '@contember/schema'

return 'date'
case Model.ColumnType.Time:
return 'time'
case Model.ColumnType.Json:

@@ -22,0 +24,0 @@ return 'jsonb'

@@ -70,2 +70,3 @@ import * as Typesafe from '@contember/typesafe'

case Model.ColumnType.DateTime:
case Model.ColumnType.Time:
case Model.ColumnType.Uuid:

@@ -72,0 +73,0 @@ case Model.ColumnType.Enum:

@@ -133,3 +133,4 @@ import * as Typesafe from '@contember/typesafe'

typeAlias: Typesafe.string,
default: Typesafe.union(Typesafe.string, Typesafe.number, Typesafe.boolean, Typesafe.null_),
list: Typesafe.boolean,
default: Typesafe.anyJson,
sequence: intersectionSchema as Typesafe.Type<Model.AnyColumn['sequence']>,

@@ -136,0 +137,0 @@ }),

@@ -14,2 +14,3 @@ import * as Typesafe from '@contember/typesafe'

shortDateResponse: Typesafe.boolean,
uuidVersion: Typesafe.union(Typesafe.literal(4), Typesafe.literal(7)),
}),

@@ -16,0 +17,0 @@ })

export type ValidationErrorCode =
| 'MODEL_NAME_MISMATCH'
| 'MODEL_UNDEFINED_FIELD'
| 'MODEL_INVALID_FIELD'
| 'MODEL_UNDEFINED_ENTITY'

@@ -5,0 +6,0 @@ | 'MODEL_RELATION_REQUIRED'

import { Model } from '@contember/schema'
import { ErrorBuilder, ValidationError } from './errors'
import { acceptEveryFieldVisitor, getTargetEntity, isInverseRelation, isOwningRelation } from '../model'
import { acceptEveryFieldVisitor, getTargetEntity, isColumn, isInverseRelation, isOwningRelation } from '../model'

@@ -55,4 +55,3 @@ const IDENTIFIER_PATTERN = /^[_a-zA-Z][_a-zA-Z0-9]*$/

this.validateUniqueConstraints(
entity.unique,
new Set(Object.keys(entity.fields)),
entity,
errors.for('unique'),

@@ -63,7 +62,10 @@ )

private validateUniqueConstraints(uniqueConstraints: Model.Entity['unique'], fields: Set<string>, errors: ErrorBuilder): void {
for (const constraint of uniqueConstraints) {
private validateUniqueConstraints(entity: Model.Entity, errors: ErrorBuilder): void {
for (const constraint of entity.unique) {
for (const field of constraint.fields) {
if (!fields.has(field)) {
const fieldDef = entity.fields[field]
if (!fieldDef) {
errors.add('MODEL_UNDEFINED_FIELD', `Referenced field ${field} in a constraint does not exists`)
} else if (isColumn(fieldDef) && fieldDef.list) {
errors.add('MODEL_INVALID_FIELD', `Field ${field} in a unique constraint cannot be an array`)
}

@@ -70,0 +72,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

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

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

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

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

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

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

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

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

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

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

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

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