Skip to content

Commit

Permalink
feat(calc): sf12
Browse files Browse the repository at this point in the history
  • Loading branch information
nckhell committed Jan 2, 2025
1 parent e390519 commit 3db46de
Show file tree
Hide file tree
Showing 156 changed files with 3,303 additions and 3,378 deletions.
18 changes: 9 additions & 9 deletions src/classes/__tests__/Score.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ describe('Score', () => {
input_definition: [
{
id: 'simpleNumberInput',
input_type: {
type: {
type: 'number',
required: false,
},
},
{
id: 'simpleNumberInputWithRange',
input_type: {
type: {
type: 'number',
range: {
min: {
Expand All @@ -185,7 +185,7 @@ describe('Score', () => {
},
{
id: 'enumNumberInput',
input_type: {
type: {
type: 'number',
required: false,
allowed_answers: [
Expand All @@ -197,21 +197,21 @@ describe('Score', () => {
},
{
id: 'dateInput',
input_type: {
type: {
type: 'date',
required: false,
},
},
{
id: 'stringInput',
input_type: {
type: {
type: 'string',
required: false,
},
},
{
id: 'enumStringInput',
input_type: {
type: {
type: 'string',
required: false,
allowed_answers: [
Expand All @@ -229,7 +229,7 @@ describe('Score', () => {
},
{
id: 'booleanInput',
input_type: {
type: {
type: 'boolean',
required: false,
allowed_answers: [
Expand All @@ -250,7 +250,7 @@ describe('Score', () => {
},
{
id: 'numbersArrayInput',
input_type: {
type: {
type: 'numbers_array',
required: false,
allowed_answers: [
Expand All @@ -268,7 +268,7 @@ describe('Score', () => {
},
{
id: 'stringsArrayInput',
input_type: {
type: {
type: 'strings_array',
required: false,
allowed_answers: [
Expand Down
2 changes: 1 addition & 1 deletion src/docs/swagger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ export const swaggerDefinition = {
type: 'string',
},
label: { $ref: '#/components/schemas/label' },
input_type: {
type: {
oneOf: [
{
$ref: '#/components/schemas/booleanInputType',
Expand Down
4 changes: 3 additions & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export * from './getUnionType'
export * from './zod/getUnionType'
export * from './zod/stripOptionalFromZodObject'
export * from './parseReadmeToHtml'
export * from './createZodObjectFromInputSchema'
export * from './parseToApiSchema'
export * from './shared'
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('inputSchemaToApiInputSchema', () => {
{
id: 'inputId',
label: { en: 'inputId' },
input_type: {
type: {
type: 'boolean',
required: true,
allowed_answers: [
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('inputSchemaToApiInputSchema', () => {
{
id: 'inputId',
label: { en: 'inputId' },
input_type: {
type: {
type: 'boolean',
required: false,
allowed_answers: [
Expand All @@ -75,7 +75,7 @@ describe('inputSchemaToApiInputSchema', () => {
{
id: 'inputId',
label: { en: 'inputId' },
input_type: {
type: {
type: 'date',
required: false,
},
Expand All @@ -98,7 +98,7 @@ describe('inputSchemaToApiInputSchema', () => {
{
id: 'inputId',
label: { en: 'inputId' },
input_type: {
type: {
type: 'string',
required: true,
},
Expand All @@ -124,7 +124,7 @@ describe('inputSchemaToApiInputSchema', () => {
{
id: 'inputId',
label: { en: 'inputId' },
input_type: {
type: {
type: 'string',
required: true,
allowed_answers: [
Expand Down Expand Up @@ -162,7 +162,7 @@ describe('inputSchemaToApiInputSchema', () => {
{
id: 'inputId',
label: { en: 'inputId' },
input_type: {
type: {
type: 'string',
required: false,
allowed_answers: [
Expand Down Expand Up @@ -191,7 +191,7 @@ describe('inputSchemaToApiInputSchema', () => {
{
id: 'inputId',
label: { en: 'inputId' },
input_type: { type: 'number', required: true },
type: { type: 'number', required: true },
},
])
})
Expand All @@ -213,7 +213,7 @@ describe('inputSchemaToApiInputSchema', () => {
{
id: 'inputId',
label: { en: 'inputId' },
input_type: {
type: {
type: 'number',
required: false,
component: 'slider',
Expand All @@ -237,7 +237,7 @@ describe('inputSchemaToApiInputSchema', () => {
{
id: 'inputId',
label: { en: 'inputId' },
input_type: {
type: {
type: 'number',
required: true,
range: { min: { value: 10 } },
Expand All @@ -259,7 +259,7 @@ describe('inputSchemaToApiInputSchema', () => {
{
id: 'inputId',
label: { en: 'inputId' },
input_type: {
type: {
type: 'number',
required: true,
range: { max: { value: 999 } },
Expand All @@ -281,7 +281,7 @@ describe('inputSchemaToApiInputSchema', () => {
{
id: 'inputId',
label: { en: 'inputId' },
input_type: {
type: {
type: 'number',
required: true,
range: {
Expand Down Expand Up @@ -311,7 +311,7 @@ describe('inputSchemaToApiInputSchema', () => {
{
id: 'inputId',
label: { en: 'inputId' },
input_type: {
type: {
type: 'number',
required: true,
allowed_answers: [{ value: 1 }, { value: 2 }, { value: 3 }],
Expand Down Expand Up @@ -341,7 +341,7 @@ describe('inputSchemaToApiInputSchema', () => {
{
id: 'inputId',
label: { en: 'inputId' },
input_type: {
type: {
type: 'number',
required: false,
allowed_answers: [
Expand Down Expand Up @@ -376,7 +376,7 @@ describe('inputSchemaToApiInputSchema', () => {
{
id: 'inputId',
label: { en: 'inputId' },
input_type: {
type: {
type: 'strings_array',
required: false,
allowed_answers: [
Expand Down Expand Up @@ -409,7 +409,7 @@ describe('inputSchemaToApiInputSchema', () => {
{
id: 'inputId',
label: { en: 'inputId' },
input_type: {
type: {
type: 'numbers_array',
required: true,
allowed_answers: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ScoreInputType,
} from '../../../../../types'
import { ApiInputType } from '../../types'
import { getUnionType } from '../../../../getUnionType'
import { getUnionType } from '../../../../zod/getUnionType'

const getBooleanAllowedAnswers = (
value: ScoreInputType,
Expand Down Expand Up @@ -84,7 +84,7 @@ export const inputSchemaToApiInputSchema = (
if (inputType instanceof z.ZodBoolean) {
jsonSchema[key] = {
...baseJson,
input_type: {
type: {
type: 'boolean',
required: !isOptional,
allowed_answers: getBooleanAllowedAnswers(value),
Expand Down Expand Up @@ -118,7 +118,7 @@ export const inputSchemaToApiInputSchema = (

jsonSchema[key] = {
...baseJson,
input_type: {
type: {
...tempSchema,
required: !isOptional,
// @ts-expect-error this is fine
Expand All @@ -133,15 +133,15 @@ export const inputSchemaToApiInputSchema = (
if (isDate) {
jsonSchema[key] = {
...baseJson,
input_type: {
type: {
type: 'date',
required: !isOptional,
},
}
} else {
jsonSchema[key] = {
...baseJson,
input_type: {
type: {
type: 'string',
required: !isOptional,
},
Expand All @@ -156,7 +156,7 @@ export const inputSchemaToApiInputSchema = (
if (unionType === 'string') {
jsonSchema[key] = {
...baseJson,
input_type: {
type: {
type: 'string',
required: !isOptional,
allowed_answers: getStringEnumAllowedAnswers(inputType, value),
Expand All @@ -167,7 +167,7 @@ export const inputSchemaToApiInputSchema = (
if (unionType === 'number') {
jsonSchema[key] = {
...baseJson,
input_type: {
type: {
type: 'number',
required: !isOptional,
allowed_answers: getNumberEnumAllowedAnswers(inputType, value),
Expand All @@ -187,7 +187,7 @@ export const inputSchemaToApiInputSchema = (
if (unionType === 'string') {
jsonSchema[key] = {
...baseJson,
input_type: {
type: {
type: 'strings_array',
required: !isOptional,
allowed_answers: getStringEnumAllowedAnswers(itemType, value),
Expand All @@ -198,7 +198,7 @@ export const inputSchemaToApiInputSchema = (
if (unionType === 'number') {
jsonSchema[key] = {
...baseJson,
input_type: {
type: {
type: 'numbers_array',
required: !isOptional,
allowed_answers: getNumberEnumAllowedAnswers(itemType, value),
Expand Down
2 changes: 1 addition & 1 deletion src/lib/parseToApiSchema/awell/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export type PossibleApiInputTypes =
export interface ApiInputType {
id: string
label?: LabelType
input_type: PossibleApiInputTypes
type: PossibleApiInputTypes
format?: string
info?: LabelType
}
Expand Down
20 changes: 20 additions & 0 deletions src/lib/shared.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
import _ from 'lodash'

export const is_numeric = (val: unknown): boolean =>
typeof val === 'number' && !Number.isNaN(val)

type RemoveUndefined<T> = {
[K in keyof T]-?: Exclude<T[K], undefined>
}

/**
* Checks if all values in the object are defined and refines the type.
* @param data - The object to check.
* @returns True if all values are defined; false otherwise.
*/
export const areAllValuesDefined = <T extends Record<string, unknown>>(
data: T,
): data is RemoveUndefined<T> => {
if (_.isEmpty(data)) {
return false
}
return Object.values(data).every(value => value !== undefined)
}
File renamed without changes.
File renamed without changes.
23 changes: 23 additions & 0 deletions src/lib/zod/stripOptionalFromZodObject.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { z, ZodTypeAny } from 'zod'

/**
* Removes all optional types from a Zod object schema.
* @param schema - The Zod object schema to process.
* @returns A new Zod object schema with optional types removed.
*/
export const stripOptionalFromZodObject = <T extends z.ZodObject<any>>(
schema: T,
): z.ZodObject<any> => {
const shape = schema.shape // Get the shape of the Zod object schema

const strippedShape: Record<string, ZodTypeAny> = Object.fromEntries(
Object.entries(shape).map(([key, value]) => {
if (value instanceof z.ZodOptional) {
return [key, value.unwrap()] // Unwrap optional types
}
return [key, value]
}),
)

return z.object(strippedShape) // Return a new Zod object schema
}
3 changes: 1 addition & 2 deletions src/scores/blcs/blcs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _ from 'lodash'
import { is_numeric } from '../../lib/shared'
import { BLCS_INPUTS, BLCS_OUTPUT } from './definition'
import { ScoreType } from '../../types'

Expand All @@ -9,7 +8,7 @@ export const blcs: ScoreType<typeof BLCS_INPUTS, typeof BLCS_OUTPUT> = {
inputSchema: BLCS_INPUTS,
outputSchema: BLCS_OUTPUT,
calculate: ({ data }) => {
const valid_answers = Object.values(data).filter(is_numeric)
const valid_answers = Object.values(data).filter(d => d !== undefined)

if (valid_answers.length === 0) return { BLCS_TOTAL_SCORE: null }

Expand Down
Loading

0 comments on commit 3db46de

Please sign in to comment.