Skip to content

Commit 51cc733

Browse files
MForivladfrangu
authored andcommitted
feat: handle sub-schema validation (#853)
1 parent d6a9e9f commit 51cc733

File tree

7 files changed

+197
-56
lines changed

7 files changed

+197
-56
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
"dependencies": {
6262
"@apify/actor-templates": "^0.1.5",
6363
"@apify/consts": "^2.36.0",
64-
"@apify/input_schema": "^3.12.0",
64+
"@apify/input_schema": "^3.17.0",
6565
"@apify/utilities": "^2.15.1",
6666
"@crawlee/memory-storage": "^3.12.0",
6767
"@oclif/core": "~4.3.0",

src/commands/run.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import { getAjvValidator, getDefaultsFromInputSchema, readInputSchema } from '..
2929
import { error, info, warning } from '../lib/outputs.js';
3030
import { replaceSecretsValue } from '../lib/secrets.js';
3131
import {
32-
Ajv,
32+
Ajv2019,
3333
checkIfStorageIsEmpty,
3434
getLocalInput,
3535
getLocalKeyValueStorePath,
@@ -456,7 +456,7 @@ export class RunCommand extends ApifyCommand<typeof RunCommand> {
456456
}
457457

458458
// Step 1: validate the input schema
459-
const validator = new Ajv({ strict: false, unicodeRegExp: false });
459+
const validator = new Ajv2019({ strict: false, unicodeRegExp: false });
460460
validateInputSchema(validator, inputSchema); // This one throws an error in a case of invalid schema.
461461

462462
const defaults = getDefaultsFromInputSchema(inputSchema);

src/commands/validate-schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ApifyCommand } from '../lib/apify_command.js';
88
import { LOCAL_CONFIG_PATH } from '../lib/consts.js';
99
import { readInputSchema } from '../lib/input_schema.js';
1010
import { info, success } from '../lib/outputs.js';
11-
import { Ajv } from '../lib/utils.js';
11+
import { Ajv2019 } from '../lib/utils.js';
1212

1313
export class ValidateInputSchemaCommand extends ApifyCommand<typeof ValidateInputSchemaCommand> {
1414
static override description = `Validates Actor input schema from one of these locations (in priority order):
@@ -44,7 +44,7 @@ export class ValidateInputSchemaCommand extends ApifyCommand<typeof ValidateInpu
4444
info({ message: `Validating input schema embedded in '${LOCAL_CONFIG_PATH}'` });
4545
}
4646

47-
const validator = new Ajv({ strict: false });
47+
const validator = new Ajv2019({ strict: false });
4848
validateInputSchema(validator, inputSchema); // This one throws an error in a case of invalid schema.
4949
success({ message: 'Input schema is valid.' });
5050
}

src/lib/input_schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { validateInputSchema } from '@apify/input_schema';
99

1010
import { ACTOR_SPECIFICATION_FOLDER } from './consts.js';
1111
import { warning } from './outputs.js';
12-
import { Ajv, getJsonFileContent, getLocalConfig, getLocalKeyValueStorePath } from './utils.js';
12+
import { Ajv2019, getJsonFileContent, getLocalConfig, getLocalKeyValueStorePath } from './utils.js';
1313

1414
const DEFAULT_INPUT_SCHEMA_PATHS = [
1515
'.actor/INPUT_SCHEMA.json',
@@ -85,7 +85,7 @@ export const createPrefilledInputFileFromInputSchema = async (actorFolderDir: st
8585
* It is not possible to install the package here because it is private
8686
* We should move it to @apify/input_schema and use it from there.
8787
*/
88-
const validator = new Ajv({ strict: false });
88+
const validator = new Ajv2019({ strict: false });
8989
validateInputSchema(validator, inputSchema);
9090

9191
// inputFile = _.mapObject(inputSchema.properties as any, (fieldSchema) =>

src/lib/utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { finished } from 'node:stream/promises';
99
import { DurationFormatter as SapphireDurationFormatter, TimeTypes } from '@sapphire/duration';
1010
import { Timestamp } from '@sapphire/timestamp';
1111
import AdmZip from 'adm-zip';
12-
import _Ajv from 'ajv';
12+
import _Ajv2019 from 'ajv/dist/2019.js';
1313
import { type ActorRun, ApifyClient, type ApifyClientOptions, type Build } from 'apify-client';
1414
import archiver from 'archiver';
1515
import { AxiosHeaders } from 'axios';
@@ -48,7 +48,7 @@ import type { AuthJSON } from './types.js';
4848

4949
// Export AJV properly: https://github.com/ajv-validator/ajv/issues/2132
5050
// Welcome to the state of JavaScript/TypeScript and CJS/ESM interop.
51-
export const Ajv = _Ajv as unknown as typeof import('ajv').default;
51+
export const Ajv2019 = _Ajv2019 as unknown as typeof import('ajv/dist/2019.js').default;
5252

5353
export const httpsGet = async (url: string) => {
5454
return new Promise<IncomingMessage>((resolve, reject) => {

test/__setup__/input-schemas/valid.json

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,29 @@
5656
"default": false,
5757
"groupCaption": "Options",
5858
"groupDescription": "Various options for this Actor"
59-
}
59+
},
60+
"person": {
61+
"title": "Person",
62+
"type": "object",
63+
"description": "An example of a nested object",
64+
"editor": "schemaBased",
65+
"properties": {
66+
"firstName": {
67+
"type": "string",
68+
"title": "First Name",
69+
"description": "First name of the person",
70+
"editor": "textfield"
71+
},
72+
"lastName": {
73+
"type": "string",
74+
"title": "Last Name",
75+
"description": "Last name of the person",
76+
"editor": "textfield"
77+
}
78+
},
79+
"additionalProperties": false,
80+
"required": ["lastName"]
81+
}
6082
},
6183
"required": ["queries"]
6284
}

0 commit comments

Comments
 (0)