Skip to content

Commit

Permalink
[STRATCONN-4162]:Add min/max validations for number type (#2358)
Browse files Browse the repository at this point in the history
* Test commit

* Test commit

* Test commit

* Remove Faulty Test Assetions
  • Loading branch information
itsarijitray authored Sep 23, 2024
1 parent f3c03cd commit c06ae0b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/core/src/__tests__/schema-validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,4 +239,22 @@ describe('validateSchema', () => {
}
`)
})

it('should validate min/max for number type fields', () => {
const less_than_min_payload = { batch_size: 99 }
const greater_than_max_payload = { batch_size: Number.MAX_SAFE_INTEGER }
const valid_number_payload = { batch_size: 158 }

const min_max_schema = fieldsToJsonSchema({
batch_size: {
type: 'number',
label: 'Batch size',
minimum: 100,
maximum: 10000
}
})
expect(validateSchema(valid_number_payload, min_max_schema)).toBeTruthy()
expect(validateSchema(less_than_min_payload, min_max_schema, { throwIfInvalid: false })).toBeFalsy()
expect(validateSchema(greater_than_max_payload, min_max_schema, { throwIfInvalid: false })).toBeFalsy()
})
})
8 changes: 8 additions & 0 deletions packages/core/src/destination-kit/fields-to-jsonschema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ export function fieldsToJsonSchema(fields: MinimalFields = {}, options?: SchemaO
schema.format = 'password'
} else if (field.type === 'text') {
schema.format = 'text'
} else if (field.type === 'number') {
const { minimum = null, maximum = null } = field as InputField
if (minimum) {
schema.minimum = (field as InputField)?.minimum
}
if (maximum) {
schema.maximum = (field as InputField)?.maximum
}
}

if (field.choices) {
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/destination-kit/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ export interface InputField extends InputFieldJSONSchema {
* Determines which input methods are disabled for this field. This is useful when you want to restrict variable selection, freeform entry, etc.
*/
disabledInputMethods?: FieldInputMethods[]

/** Minimum value for a field of type 'number' */
minimum?: number
/** Maximum value for a field of type 'number' */
maximum?: number
}

/** Base interface for conditions */
Expand Down

0 comments on commit c06ae0b

Please sign in to comment.