Began experimenting with dragging typings out of simple schema

This commit is contained in:
ThaumRystra
2025-01-01 22:39:54 +02:00
parent e76ad64a7d
commit fcf6a84b01
9 changed files with 163 additions and 59 deletions

View File

@@ -7,6 +7,7 @@ import { CreatureProperty } from '/imports/api/creature/creatureProperties/Creat
import { InlineCalculation } from '/imports/api/properties/subSchemas/inlineCalculationField';
import { CalculatedField } from '/imports/api/properties/subSchemas/computedField';
import Property from '/imports/api/properties/Properties.type';
import { TypedSimpleSchema } from '/imports/api/utility/TypedSimpleSchema';
export type CreatureAction = Action & CreatureProperty & {
overridden?: boolean
@@ -314,7 +315,7 @@ const ComputedOnlyActionSchema = createPropertySchema({
},
});
const ComputedActionSchema = new SimpleSchema()
const ComputedActionSchema = new TypedSimpleSchema({})
.extend(ActionSchema)
.extend(ComputedOnlyActionSchema);

View File

@@ -1,10 +1,10 @@
import SimpleSchema from 'simpl-schema';
import { Random } from 'meteor/random';
import { InferType, TypedSimpleSchema } from '/imports/api/utility/TypedSimpleSchema';
const AdjustmentSchema = new SimpleSchema({
const AdjustmentSchema = new TypedSimpleSchema({
_id: {
type: String,
regEx: SimpleSchema.RegEx.Id,
max: 17,
autoValue() {
if (!this.isSet) return Random.id();
}
@@ -23,7 +23,7 @@ const AdjustmentSchema = new SimpleSchema({
'self', // the character who took the action
'each', // rolled once for `each` target
'every', // rolled once and applied to `every` target
],
] as const,
},
// The stat this rolls applies to, if damage type is set, this is ignored
stat: {
@@ -32,4 +32,6 @@ const AdjustmentSchema = new SimpleSchema({
},
});
export type Adjustment = InferType<typeof AdjustmentSchema>;
export default AdjustmentSchema;

View File

@@ -1,10 +1,6 @@
import SimpleSchema from 'simpl-schema';
import { TypedSimpleSchema } from '/imports/api/utility/TypedSimpleSchema';
export interface Colored {
color?: string,
}
const ColorSchema = new SimpleSchema({
const ColorSchema = new TypedSimpleSchema({
color: {
type: String,
// match hex colors of the form #A23 or #A23f56

View File

@@ -6,12 +6,12 @@ import {
fieldToCompute,
computedOnlyField,
} from '/imports/api/properties/subSchemas/computedField';
import SimpleSchema from 'simpl-schema';
import { Definition, TypedSimpleSchema } from '/imports/api/utility/TypedSimpleSchema';
// Search through the schema for keys whose type is 'fieldToCompute' etc.
// replace the type with Object and attach extend the schema with
// the required fields to make the computation work
export default function createPropertySchema(definition) {
export default function createPropertySchema(definition: Definition) {
const computationFields = {
inlineCalculationFieldToCompute: [],
computedOnlyInlineCalculationField: [],
@@ -20,9 +20,9 @@ export default function createPropertySchema(definition) {
};
const computedKeys = Object.keys(computationFields);
for (let key in definition) {
for (const key in definition) {
const def = definition[key];
if (computedKeys.includes(def.type)) {
if (typeof def === 'object' && 'type' in def && computedKeys.includes(def.type)) {
computationFields[def.type].push(key);
applyDefaultCalculationValue(definition, key);
def.type = Object;
@@ -31,6 +31,7 @@ export default function createPropertySchema(definition) {
`computed field: '${key}' of '${def.type}' is expected to be optional`
);
}
//@ts-expect-error removeBeforeCompute is an extension of SimpleSchema
if (def.removeBeforeCompute) {
console.warn(
`computed field: '${key}' of '${def.type}' should not be removed before computation`
@@ -40,7 +41,7 @@ export default function createPropertySchema(definition) {
}
// Create a schema with the edited definition
const schema = new SimpleSchema(definition);
const schema = new TypedSimpleSchema(definition);
// Extend the schema with all the computation fields
computationFields.inlineCalculationFieldToCompute.forEach(key => {
@@ -69,7 +70,7 @@ function applyDefaultCalculationValue(definition, key) {
// on the fields to compute
return;
}
let defaultValue = def.defaultValue;
const defaultValue = def.defaultValue;
if (!defaultValue) return;
let calcField;
if (def.type === 'fieldToCompute') {