Skip to content

feat(core): dev schema #446

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
16 changes: 15 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@dfinity/candid": "^2.3.0",
"@dfinity/identity": "^2.3.0",
"@dfinity/principal": "^2.3.0",
"@dfinity/utils": "^2"
"@dfinity/utils": "^2",
"@standard-schema/spec": "^1.0.0"
}
}
1 change: 1 addition & 0 deletions packages/core/src/types/doc-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type DocDataSchema = {}

Check failure on line 1 in packages/core/src/types/doc-data.ts

View workflow job for this annotation

GitHub Actions / lint

Use an `interface` instead of a `type`
12 changes: 12 additions & 0 deletions packages/core/src/types/errors.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import {StandardSchemaV1} from '@standard-schema/spec';

Check failure on line 1 in packages/core/src/types/errors.ts

View workflow job for this annotation

GitHub Actions / lint

All imports in the declaration are only used as types. Use `import type`

export class SignInError extends Error {}
export class SignInInitError extends Error {}
export class SignInUserInterruptError extends Error {}

export class InitError extends Error {}

export class ListError extends Error {}

export class SchemaError extends Error {
readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;

constructor(issues: ReadonlyArray<StandardSchemaV1.Issue>) {
super(issues[0]?.message);

this.issues = issues;
}
}
44 changes: 44 additions & 0 deletions packages/core/src/utils/schema.utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import {nonNullish} from '@dfinity/utils';
import type {StandardSchemaV1} from '@standard-schema/spec';
import {SchemaError} from '../types/errors';

export const assertDataSchema = async <T extends StandardSchemaV1>({
input,
schema
}: {
input: StandardSchemaV1.InferInput<T>;
schema?: T;
}): Promise<void> => {
const withSchema =
nonNullish(schema) && nonNullish(schema['~standard']) && 'validate' in schema['~standard'];

if (withSchema) {
await validateSchema({input, schema});
}
};

const validateSchema = async <T extends StandardSchemaV1>({
input,
schema
}: {
input: StandardSchemaV1.InferInput<T>;
schema: T;
}): Promise<StandardSchemaV1.InferOutput<T>> => {
const validate = async (): Promise<StandardSchemaV1.Result<unknown>> => {
const result = schema['~standard'].validate(input);

if (result instanceof Promise) {
return await result;
}

return result;
};

const {issues, ...value} = await validate();

if (issues !== undefined) {
throw new SchemaError(issues);
}

return value;
};
Loading