question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature] Statically set schemas

See original GitHub issue

It would be great if we could do something like this:

import { buildSchema } from '@camberi/firecms';
import { Category } from "@my-projects/schemas";

export const categorySchema = buildSchema<Category>({
 /** everything here stays the same, but now, typescript complains if the schema mismatches with the Category. **/
});

Thanks a lot for your work here 💯

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
fgatti675commented, May 6, 2021

Hi @PupoSDC Thanks a lot for your PR!! I have merged it but modified it a little bit. The function you need is now called buildSchemaFrom and I have modified it slightly. I have also tried checking the property data types based on the Typescript types but failed! I already tried once and failed as well, I have to admit Typescript has defeated me here! I am pretty sure it’s possible but I am getting something wrong.

It is not released yet, but it is merged into the master branch so if you would like to use it you can link your project to it directly.

If anyone sees this and is willing to try, the goal is to be able to type check property data types based on a given Typescript type.

A simplified version of the model would be:

interface EntitySchema<Key extends string = string, T extends any = any> {
    properties: Record<Key, Property<T>>;
}

type Property<T = any> =
    T extends string ? StringProperty :
        T extends number ? NumberProperty : never;


interface StringProperty {
    dataType: "string";
}

interface NumberProperty {
    dataType: "number";
}

function buildSchemaFrom<Type extends Partial<{ [P in Key]: T; }>,
    Key extends string = Extract<keyof Type, string>,
    T = any>(
    schema: EntitySchema<Key, T>
): EntitySchema<Key, T> {
    return schema;
}

type Product = {
    name: string,
    price: number,
}

export const productSchema = buildSchemaFrom<Product>({
    properties: {
        name: {
            dataType: "string"
        },
        price: {
            dataType: "string", // THIS SHOULD FAIL
        },
        // missing: { // This fails if uncommented, which is expected
        //     dataType: "string",
        // },
    }
});

If anyone manages to get the simplified version working we can probably escalate it to the whole app 😃

2reactions
fgatti675commented, May 3, 2021

Ah I understand! So getting statically type checks based on your Typescript interfaces. Makes total sense, and would make a nice enhancement. I’m adding it to the backlog. If you feel like giving a shot at the implementation let me know!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Feature and table schemas for task parameters—ArcGIS Server
Input schemas based on Feature and Record Sets ... Custom model and script tools you create can use the Feature Set or the...
Read more >
Mongoose v6.8.1: Schemas
Each schema maps to a MongoDB collection and defines the shape of the documents ... new Schema({ name: String, type: String }, {...
Read more >
Using the Schema Discovery Feature on Static Data
The schema discovery feature can generate a schema from either the data in a stream or data in a static file that is...
Read more >
Static variables declared on advanced schemas are not ...
When declaring a static variable on a class loaded with loadClass into the schema, the static variable does not get loaded into the...
Read more >
Documentation: 15: CREATE FUNCTION - PostgreSQL
If a schema name is included, then the function is created in the specified schema. Otherwise it is created in the current schema....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found