|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 The Ontario Institute for Cancer Research. All rights reserved |
| 3 | + * |
| 4 | + * This program and the accompanying materials are made available under the terms of |
| 5 | + * the GNU Affero General Public License v3.0. You should have received a copy of the |
| 6 | + * GNU Affero General Public License along with this program. |
| 7 | + * If not, see <http://www.gnu.org/licenses/>. |
| 8 | + * |
| 9 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 10 | + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 11 | + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 12 | + * SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 13 | + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 14 | + * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 15 | + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 16 | + * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 17 | + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 18 | + */ |
| 19 | + |
| 20 | +import { expect } from 'chai'; |
| 21 | +import { |
| 22 | + BooleanFieldRestrictions, |
| 23 | + Integer, |
| 24 | + IntegerFieldRestrictions, |
| 25 | + NumberFieldRestrictions, |
| 26 | + RestrictionIntegerRange, |
| 27 | + RestrictionNumberRange, |
| 28 | + SchemaField, |
| 29 | + StringFieldRestrictions, |
| 30 | + type ConditionalRestriction, |
| 31 | + type SchemaStringField, |
| 32 | + type StringFieldRestrictionsObject, |
| 33 | +} from '../../src'; |
| 34 | +import assert from 'assert'; |
| 35 | + |
| 36 | +describe('Restriction Schemas', () => { |
| 37 | + describe('Integer', () => { |
| 38 | + it("Can't be float", () => { |
| 39 | + expect(Integer.safeParse(1.3).success).false; |
| 40 | + expect(Integer.safeParse(2.0000001).success).false; |
| 41 | + // Note: float precision issues, if the float resolves to a whole number the value will be accepted. |
| 42 | + }); |
| 43 | + it("Can't be string, boolean, object, array", () => { |
| 44 | + expect(Integer.safeParse('1').success).false; |
| 45 | + expect(Integer.safeParse(true).success).false; |
| 46 | + expect(Integer.safeParse([1]).success).false; |
| 47 | + expect(Integer.safeParse({}).success).false; |
| 48 | + expect(Integer.safeParse({ thing: 1 }).success).false; |
| 49 | + }); |
| 50 | + it('Can be integer', () => { |
| 51 | + expect(Integer.safeParse(1).success).true; |
| 52 | + expect(Integer.safeParse(0).success).true; |
| 53 | + expect(Integer.safeParse(-1).success).true; |
| 54 | + expect(Integer.safeParse(1123).success).true; |
| 55 | + }); |
| 56 | + }); |
| 57 | + describe('RangeRestriction', () => { |
| 58 | + it("Integer Range Can't have exclusiveMin and Min", () => { |
| 59 | + expect(RestrictionIntegerRange.safeParse({ exclusiveMin: 0, min: 0 }).success).false; |
| 60 | + expect(RestrictionIntegerRange.safeParse({ min: 0 }).success).true; |
| 61 | + expect(RestrictionIntegerRange.safeParse({ exclusiveMin: 0 }).success).true; |
| 62 | + }); |
| 63 | + it("Integer Range Can't have exclusiveMax and Max", () => { |
| 64 | + expect(RestrictionIntegerRange.safeParse({ exclusiveMax: 0, max: 0 }).success).false; |
| 65 | + expect(RestrictionIntegerRange.safeParse({ max: 0 }).success).true; |
| 66 | + expect(RestrictionIntegerRange.safeParse({ exclusiveMax: 0 }).success).true; |
| 67 | + }); |
| 68 | + it("Number Range Can't have exclusiveMin and Min", () => { |
| 69 | + expect(RestrictionNumberRange.safeParse({ exclusiveMin: 0, min: 0 }).success).false; |
| 70 | + expect(RestrictionNumberRange.safeParse({ min: 0 }).success).true; |
| 71 | + expect(RestrictionNumberRange.safeParse({ exclusiveMin: 0 }).success).true; |
| 72 | + }); |
| 73 | + it("Number Range Can't have exclusiveMax and Max", () => { |
| 74 | + expect(RestrictionNumberRange.safeParse({ exclusiveMax: 0, max: 0 }).success).false; |
| 75 | + expect(RestrictionNumberRange.safeParse({ max: 0 }).success).true; |
| 76 | + expect(RestrictionNumberRange.safeParse({ exclusiveMax: 0 }).success).true; |
| 77 | + }); |
| 78 | + }); |
| 79 | + describe('RegexRestriction', () => { |
| 80 | + it('Accepts valid regex', () => { |
| 81 | + expect(StringFieldRestrictions.safeParse({ regex: '[a-zA-Z]' }).success).true; |
| 82 | + expect( |
| 83 | + StringFieldRestrictions.safeParse({ |
| 84 | + regex: '^({((([WUBRG]|([0-9]|[1-9][0-9]*))(/[WUBRG])?)|(X)|([WUBRG](/[WUBRG])?/[P]))})+$', |
| 85 | + }).success, |
| 86 | + ).true; |
| 87 | + }); |
| 88 | + it('Rejects invalid regex', () => { |
| 89 | + expect(StringFieldRestrictions.safeParse({ regex: '[' }).success).false; |
| 90 | + }); |
| 91 | + }); |
| 92 | + describe('UniqueRestriction', () => { |
| 93 | + it('All fields accept unique restriction', () => { |
| 94 | + expect(StringFieldRestrictions.safeParse({ unique: true }).success).true; |
| 95 | + expect(NumberFieldRestrictions.safeParse({ unique: true }).success).true; |
| 96 | + expect(IntegerFieldRestrictions.safeParse({ unique: true }).success).true; |
| 97 | + expect(BooleanFieldRestrictions.safeParse({ unique: true }).success).true; |
| 98 | + }); |
| 99 | + }); |
| 100 | + describe('ScriptRestriction', () => { |
| 101 | + it('All fields accept script restriction', () => { |
| 102 | + expect(StringFieldRestrictions.safeParse({ script: ['()=>true'] }).success).true; |
| 103 | + expect(NumberFieldRestrictions.safeParse({ script: ['()=>true'] }).success).true; |
| 104 | + expect(IntegerFieldRestrictions.safeParse({ script: ['()=>true'] }).success).true; |
| 105 | + expect(BooleanFieldRestrictions.safeParse({ script: ['()=>true'] }).success).true; |
| 106 | + }); |
| 107 | + }); |
| 108 | + |
| 109 | + describe('ConditionalRestrictions', () => { |
| 110 | + // These parsing functions seem unnecessary but they are checking for a failure case that was found: |
| 111 | + // The restrictions property is a union between a restrictions object and conditional restriction schema, |
| 112 | + // and the restriction object has all optional fields, so it will match with a conditional restriction |
| 113 | + // object successfully and strip out the if/then/else properties. To avoid this scenario, the RestrictionObject |
| 114 | + // schemas make the restriction validation `strict()`. These parsing tests are ensuring that this behaviour |
| 115 | + // is not changed. |
| 116 | + |
| 117 | + it('Parses single conditional restriction', () => { |
| 118 | + const restrictions: ConditionalRestriction<StringFieldRestrictions> = { |
| 119 | + if: { |
| 120 | + conditions: [{ fields: ['another-field'], match: { value: 'asdf' } }], |
| 121 | + }, |
| 122 | + then: { required: true }, |
| 123 | + else: { empty: true }, |
| 124 | + }; |
| 125 | + const field: SchemaStringField = { |
| 126 | + name: 'example-string', |
| 127 | + valueType: 'string', |
| 128 | + restrictions, |
| 129 | + }; |
| 130 | + const parseResult = SchemaField.safeParse(field); |
| 131 | + expect(parseResult.success).true; |
| 132 | + assert(parseResult.success === true); |
| 133 | + |
| 134 | + expect(parseResult.data).deep.equal(field); |
| 135 | + }); |
| 136 | + }); |
| 137 | + it('Parses conditional restrictions in an array', () => { |
| 138 | + const restrictions: Array<StringFieldRestrictionsObject> = [ |
| 139 | + { codeList: ['value1', 'value2'] }, |
| 140 | + { |
| 141 | + if: { |
| 142 | + conditions: [{ fields: ['another-field'], match: { value: 'asdf' } }], |
| 143 | + }, |
| 144 | + then: { required: true }, |
| 145 | + else: { empty: true }, |
| 146 | + }, |
| 147 | + ]; |
| 148 | + const field: SchemaStringField = { |
| 149 | + name: 'example-string', |
| 150 | + valueType: 'string', |
| 151 | + restrictions, |
| 152 | + }; |
| 153 | + const parseResult = SchemaField.safeParse(field); |
| 154 | + expect(parseResult.success).true; |
| 155 | + assert(parseResult.success === true); |
| 156 | + |
| 157 | + expect(parseResult.data).deep.equal(field); |
| 158 | + }); |
| 159 | + |
| 160 | + it('Parses nested conditional restrictions', () => { |
| 161 | + const restrictions: Array<StringFieldRestrictionsObject> = [ |
| 162 | + { codeList: ['value1', 'value2'] }, |
| 163 | + { |
| 164 | + if: { |
| 165 | + conditions: [{ fields: ['first-dependent-field'], match: { value: 'asdf' } }], |
| 166 | + }, |
| 167 | + then: { |
| 168 | + if: { |
| 169 | + conditions: [{ fields: ['second-dependent-field'], match: { range: { max: 10, min: 0 } } }], |
| 170 | + }, |
| 171 | + then: { required: true }, |
| 172 | + else: { empty: true }, |
| 173 | + }, |
| 174 | + else: { empty: true }, |
| 175 | + }, |
| 176 | + ]; |
| 177 | + const field: SchemaStringField = { |
| 178 | + name: 'example-string', |
| 179 | + valueType: 'string', |
| 180 | + restrictions, |
| 181 | + }; |
| 182 | + const parseResult = SchemaField.safeParse(field); |
| 183 | + expect(parseResult.success).true; |
| 184 | + assert(parseResult.success === true); |
| 185 | + |
| 186 | + expect(parseResult.data).deep.equal(field); |
| 187 | + }); |
| 188 | +}); |
0 commit comments